尽管调用了viewDidLoad,但未显示iOS ViewController

时间:2014-12-19 11:08:43

标签: ios uiviewcontroller uistoryboard appdelegate pushviewcontroller

我正在构建一个具有不同状态的应用程序,如果用户由于某种原因退出应用程序,则需要恢复这些状态。这意味着,当应用程序启动时,不同的屏幕/视图控制器需要位于导航堆栈的顶部。为了实现这一点,我尝试以编程方式构建viewcontrollers,见下文:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        switch (appState)
        {
            case CSAppStateIdle:
            {
                //replace and push rootview manually
                MainVC* mainView = [storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
                [(UINavigationController *)self.window.rootViewController pushViewController:mainView animated:YES];
            }
                break;
            case CSAppStateSomeState:
            {
                MainVC* mainView = [storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
                SomeVC* someView = [storyboard instantiateViewControllerWithIdentifier:@"SomeVC"];
                [(UINavigationController *)self.window.rootViewController addChildViewController:mainView];
                [(UINavigationController *)self.window.rootViewController pushViewController:someView animated:YES];
            }
                break;

            default:
                break;
        }

我使用ID-s设置正确的stroyboards。好吧在CSAppStateIdle中它可以工作,但在CSAppStateSomeState中,someView不会出现在屏幕上,而是在某些VC viewDidLoad,viewWillAppear甚至viewDidAppear中被调用。

以前是否有人遇到此问题,或者知道解决方案?感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

要将两个项目放在导航堆栈上,请使用setViewControllers:animated:方法,传递包含两个项目的数组:

        case CSAppStateSomeState:
        {
            MainVC* mainView = [storyboard instantiateViewControllerWithIdentifier:@"MainVC"];
            SomeVC* someView = [storyboard instantiateViewControllerWithIdentifier:@"SomeVC"];
            [(UINavigationController *)self.window.rootViewController setViewControllers:@[mainView, someView] animated:YES];
        }