【问题标题】:I expect the initial view controller of my storyboard to be the current controller我希望我的故事板的初始视图控制器是当前控制器
【发布时间】:2012-06-27 02:57:11
【问题描述】:

如果应用程序之前在后台,当调用 applicationDidBecomeActive 时,我希望我的故事板的初始视图控制器是当前控制器。

我用过:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [self.window makeKeyAndVisible];
}

当我重新启动应用程序时,rootViewController 内的loginAciton 仍然被调用,但无法呈现下一个控制器。没有错误,就像什么都没发生一样。

- (IBAction)loginAciton:(id)sender
{
    id controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Navigation"];
    [self presentModalViewController:controller animated:YES];   
}

为什么?

PS。我的 rootViewController 不是 UINavigationController。

感谢您的任何回复。

【问题讨论】:

  • [self.storyboard instantiateViewControllerWithIdentifier:@"Navigation"]返回什么?
  • 你能用-performSegueWithIdentifier:sender:代替吗?
  • 返回 UINavigationController。 -performSegueWithIdentifier:sender:presentModalViewController 之间有什么区别?
  • 对不起,我想我不是很清楚,使用 Debugger 或 NSLog() 并获取实际返回值。如果-presentModalViewController:animated: 失败,则不会返回 UINavigationController 对象。 -instantiateViewControllerWithIdentifier:-performSegueWithIdentifier:sender: 使用不同的标识符。如果您在使用一个标识符时遇到问题,那么另一个可能会起作用。

标签: iphone ios storyboard


【解决方案1】:

更好的方法是将UIApplicationExitsOnSuspend 添加到您的Info.plist 并将其设置为YES

【讨论】:

    【解决方案2】:

    我明白了!

    如果您希望故事板的初始视图控制器每次都成为当前控制器,您可以尝试一下:

    - (void)applicationDidEnterBackground:(UIApplication *)application
    {
       exit(EXIT_SUCCESS);
    }
    

    【讨论】:

    • 如果你调用exit()他们可能会拒绝你的应用,mavrick3的建议更简洁
    【解决方案3】:

    你也可以使用通知,把这段代码放在applicationDidBecomeActive:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"appActivated" object:nil];
    

    并在当前视图中添加观察者...

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(updateView:)        
                                             name:@"appActivated" 
                                           object:nil];
    

    并且不要忘记删除当前视图的 dealloc: 中的观察者...

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    

    希望对你有帮助..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多