【问题标题】:How to control view controllers in appdelegate?如何在 appdelegate 中控制视图控制器?
【发布时间】:2016-01-05 07:47:29
【问题描述】:

我真的为这个简单的问题设计了结构。

我只是使用以下代码从一个视图控制器移动到另一个视图控制器。

homescreen *home;
home=[self.storyboard instantiateViewControllerWithIdentifier:@"home"];
[self presentViewController:home animated:YES completion:nil];

AppDelegate 方法中尝试使用相同的代码。

UIStoryboard *storyboard;
sur= [storyboard instantiateViewControllerWithIdentifier:@"survey"];
[self.window.rootViewController presentViewController:sur animated:YES completion:nil];

当我使用这段代码时,我遇到了这个崩溃:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“应用程序试图在目标上呈现一个 nil 模式视图控制器

我不想让它成为根视图控制器,我只需要移动它。

【问题讨论】:

    标签: ios objective-c appdelegate


    【解决方案1】:

    你只声明了 UIStoryboard *storyboard 没有初始化它,并且在下一行你调用方法 instantiateViewControllerWithIdentifier 在 nil 对象上。最终storyboard 变量在下一行替换了 nil 并崩溃。

    sur= [storyboard instantiateViewControllerWithIdentifier:@"survey"];
    

    【讨论】:

      【解决方案2】:

      您不能直接在空的 window.rootViewController 上呈现视图。您需要先将其设置为任何 viewController,然后显示需要显示的 viewController。参考这段代码:

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
      
          self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
          UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
          InitialViewController *initialVC = [storyboard instantiateViewControllerWithIdentifier:@"InitialViewController"];
          self.window.rootViewController = initialVC;
          [self.window makeKeyAndVisible];
      
          ShowThisViewController *showThisVC = [storyboard instantiateViewControllerWithIdentifier:@"ShowThisViewController"];
          [self.window.rootViewController presentViewController:showThisVC animated:YES completion:nil];
      
          return YES; }
      

      【讨论】:

        【解决方案3】:

        访问this

        我认为阿米尔是对的。但是,此外,您需要使您的窗口可见。

        你需要打电话

        [self.window makeKeyAndVisible];
        

        在展示任何 ViewController 之前。

        【讨论】:

          【解决方案4】:

          使用此代码

              DEMORootViewController *VerifyV = [self.storyboard  instantiateViewControllerWithIdentifier:@"Identifier"];  
          
             UINavigationController *navigationController=[[UINavigationController alloc]initWithRootViewController:VerifyV];
          
              self.window.rootViewController = navigationController;
          

          【讨论】:

            【解决方案5】:

            只需尝试以下代码:

            UIStoryboard *storyboard;
            survey *sur= [storyboard instantiateViewControllerWithIdentifier:@"survey"];
            UIViewController *curViewCont = [UIApplication sharedApplication].keyWindow.rootViewController;
            while (curViewCont.presentedViewController) {
                curViewCont = curViewCont.presentedViewController;
            }
            [curViewCont presentViewController:sur animated:YES completion:nil];
            

            希望它对你有用。

            【讨论】:

            • @KishoreKumar 你还在让应用崩溃吗?
            • @KishoreKumar 它没有给我错误。我能够运行我的代码。你有没有用你的视图控制器名称代替调查?
            • @KishoreKumar 已经在我的源代码中测试了这个解决方案,我使用的是相同的,它对我很有用
            猜你喜欢
            • 1970-01-01
            • 2017-10-25
            • 1970-01-01
            • 1970-01-01
            • 2012-02-19
            • 2017-04-11
            • 2015-05-23
            • 1970-01-01
            • 2018-12-25
            相关资源
            最近更新 更多