【问题标题】:Why is the incorrect ViewController Showing? and how do I display the correct one?为什么显示不正确的 ViewController?以及如何显示正确的?
【发布时间】:2019-02-27 08:41:39
【问题描述】:

如果用户尚未注册用户名等,我会尝试将用户发送到注册页面。我无法加载注册 viewController,它只加载 MainViewController。 我已经尝试了所有我能找到的方法,但没有任何效果。我的背景是 Java/Android,所以这一切都很新。一个正确方向的点将不胜感激..

代码

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.




    BOOL hasRegistered = YES;

    if (!hasRegistered)
    {
    UIViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:NULL] instantiateViewControllerWithIdentifier:@"registrationView"];
[self presentViewController:viewController animated:YES completion:nil];
    }

}

【问题讨论】:

  • 在您当前的代码中,您刚刚初始化了 viewController。没有礼物,推送完成。
  • 我已经更新了我的代码,但它仍然什么都不做,只显示第一个视图控制器 - 我收到消息“警告:尝试在 上呈现 视图不在窗口层次结构中!”
  • 你在项目中使用了navigationController吗?
  • 是的,它确实存在

标签: ios objective-c viewcontroller


【解决方案1】:

试试这个

registrationViewController *viewController = (registrationViewController*)[[UIStoryboard storyboardWithName:@"Main" bundle:NULL] instantiateViewControllerWithIdentifier:@"registrationView"];

而不是

UIViewController *viewController = [[UIStoryboard storyboardWithName:@"Main" bundle:NULL] instantiateViewControllerWithIdentifier:@"registrationView"];

如果您以模态方式显示视图控制器,请使用此打开

[self presentViewController:viewController animated:YES completion:nil];

或者如果你使用navigationController 那么

[self.navigationController pushViewController:viewController animated:YES];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    相关资源
    最近更新 更多