【问题标题】:Switch view when press button按下按钮时切换视图
【发布时间】:2014-04-02 11:37:16
【问题描述】:

我想在按下按钮时改变视图。 在根视图.h 我有这个:

@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];works,我不知道为什么。

【问题讨论】:

  • 确保在使用 inititWithRootViewController 的 navigationController 中有 MainViewController
  • 因为没有可用的导航控制器,而不是将视图呈现为模态视图,它会工作

标签: ios view uibutton


【解决方案1】:

这是因为您没有UINavigationController 作为您的rootViewController

AppDelegate.m

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

【讨论】:

    【解决方案2】:

    尝试这样做:

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

    【讨论】:

    • 对您的答案提供一些解释可能会使 OP 和其他人受益
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多