0

我想知道是否可以在不使用 Interface Builder 的情况下制作多视图应用程序。

我知道如何仅使用 Xcode 和 Storyboards 来制作它,但我只想能够以编程方式制作它。

例如:如果我有一个UIViewControllerA(默认视图)和一个UIButton,然后我有另一个UIViewControllerB,UIWebView所以当我点击AIUIButtonUIViewController希望能够显示第二个视图(没有 IB)。

如何将操作设置UIButton为显示我的第二个视图(没有界面生成器)?

4

2 回答 2

1

您可以向按钮添加目标:

[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

然后在ControllerA中,添加这个方法:

-(void)buttonPressed:(id)sender
{
    [self.superview addSubview:controllerB.view];
    [self.view removeFromSuperview];
}
于 2013-01-07T12:59:51.720 回答
1

在 ViewControllerA 上

    - (void)viewDidLoad
    {

        UIButton  *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [btn setFrame:CGRectMake(50.0f,200.0f,60.0f,30.0f)];
        [btn setTitle:@"Next" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    -(IBAction)btnPressed:(id)sender
    {
        NextViewController *nxt=[[NextViewController alloc]initWithNibName:nil bundle:nil];
        [self.navigationController pushViewController:nxt animated:YES];
    }

在 viewControllerB 上

    - (void)viewDidLoad
    {
        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 320, 460)];    
       NSURL *targetURL = [NSURL URLWithString:@"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
        NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
        [webView loadRequest:request];

       [self.view addSubview:webView];
     }

确保在 AppDelegate.h 中应该是这样的导航控制器

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
     UINavigationController*  navController = [[UINavigationController alloc] initWithRootViewController:self.self.viewController ];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    return YES;
}
于 2013-01-07T13:01:08.560 回答