【问题标题】:iOS - If app has launched for the first time - Presenting ModalViewiOS - 如果应用程序首次启动 - 呈现 ModalView
【发布时间】:2014-08-21 16:50:03
【问题描述】:

我想出了如何在应用程序启动时呈现两个不同的视图控制器,如果我不以模态方式呈现它们,它就可以正常工作。如果我更改代码,它会以某种方式显示空白视图控制器。
从 -> self.window.rootViewController = 登陆VC; To-> [self.window.rootViewController presentViewController:landingVC animated:YES completion:nil];

这是我的应用委托中的完整代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *tabbarController = [storyboard instantiateViewControllerWithIdentifier:@"TabBarViewController"];
UIViewController *landingVC = [storyboard instantiateViewControllerWithIdentifier:@"LandingViewController"];

if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"]) {

        self.window.rootViewController = tabbarController;
 } else {
  [self.window.rootViewController presentViewController:landingVC animated:YES completion:nil];
      }

[self.window makeKeyAndVisible];
return YES;
}

AFAIK,这就是 modalVC 的显示方式。我已经验证了 storyboardID 并检查了两个视图控制器的“使用 storyboardID”。

【问题讨论】:

    标签: ios storyboard


    【解决方案1】:

    如果应用程序之前已启动,您正在做的是将根视图控制器设置为 tabbarController,否则您将告诉应用程序的根视图控制器(尚未设置且尚不存在)呈现视图控制器。

    如果你把 NSLog(@"%@", self.window.rootViewController);在 presentViewController 行上方,您将返回 nil,这意味着您正试图告诉 nil 做某事,这当然是行不通的。

    我刚刚尝试过,它有效:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.window.backgroundColor = [UIColor whiteColor];
    
        UIViewController *testRootVC = [UIViewController new];
        testRootVC.view.backgroundColor = [UIColor blueColor];
        self.window.rootViewController = testRootVC;
    
        [self.window makeKeyAndVisible];
    
        if (YES) {
            UIViewController *testModalVC = [UIViewController new];
            testModalVC.view.backgroundColor = [UIColor redColor];
            [testRootVC presentViewController:testModalVC animated:YES completion:nil];
        }
    
        return YES;
    }
    

    【讨论】:

    • 是的。我同意并且有点猜到了。但是我不确定是否先将 tabbarVC 设为 rootview,然后再展示 modalview 是否会有所帮助。有什么建议吗?
    • 看看我刚刚做的编辑。看来,您想要做的是将标签栏控制器设置为根视图,告诉窗口 makeKeyAndVisible,如果这是第一次启动,则显示模态。试试看,然后告诉我。
    • 很奇怪。它不适合我。一些调试应该修复它。谢谢。顺便说一句,您是否持有“B 许可证”?看起来你已经掌握了隧道:)
    • 从来没有听说过...这是什么?
    • 我的意思是跳伞执照。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多