按下按钮时切换视图

时间:2014-04-02 11:37:16

标签: ios view uibutton

我想在按下按钮时更改视图。 在根视图中。我有这个:

@interface MainViewController : UIViewController

-(IBAction)switchToMap;

@end

在.m我有这个:

-(IBAction)switchToMap
{
    CustomViewController *customViewController = [[CustomViewController alloc] initWithNibName:@"CustomViewController" bundle:nil];
    customViewController.title = @"Mappa";
    [self.navigationController pushViewController:customViewController animated:YES];
    //[self.view addSubview:customViewController.view];
}

代码不起作用,但如果我使用第二种方法[self.view addSubview:customViewController.view];起作用,我不知道为什么。

2 个答案:

答案 0 :(得分:0)

这是因为您没有UINavigationController作为rootViewController

AppDelegate.m

UINavigationController *navigation=[[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController=navigation;

答案 1 :(得分:0)

尝试这样做:

ViewController *CustomViewController = [[ViewController alloc] init];    
[self presentViewController:CustomViewController animated:YES completion:nil];