为什么我不能从didFinishLaunchingWithOptions启动这个模态视图?

时间:2011-03-22 01:12:38

标签: iphone objective-c ios4 uiviewcontroller

我想在我的估计中做一些非常简单的事情:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    prefs =  [NSUserDefaults standardUserDefaults];
    BOOL IsLoggedIn = [prefs boolForKey:@"IsLoggedIn"];

    if(IsLoggedIn == NO)
    {
        //Show login controller
        LoginViewController *lvc = [[LoginViewController alloc] initWithNibName:nil bundle:nil];
        [self.tabBarController presentModalViewController:lvc animated:NO];
        [lvc release];
    }
    else if(IsLoggedIn == YES)
    {
        //Continue doing crap
    }

    // Override point for customization after application launch.
    // Add the tab bar controller's current view as a subview of the window
    self.window.rootViewController = self.tabBarController;

    NSArray *tabs =  self.tabBarController.viewControllers;
    UIViewController *tbInvoice = [tabs objectAtIndex:0];
    tbInvoice.tabBarItem.image = [UIImage imageNamed:@"Open-Mail.png"];
    UIViewController *tbClient = [tabs objectAtIndex:1];
    tbClient.tabBarItem.image = [UIImage imageNamed:@"Breifcase.png"];

    [self.window makeKeyAndVisible];
    return YES;
}

使用调试器时,我看到它输入if(IsLoggedIn == NO)并运行LoginViewController代码,但视图从不显示。

这让我发疯了。

我尝试在[self.windoow makeKeyAndVisible]之后运行代码,但它没有改变任何内容。

这段代码看起来就像我见过的每个例子。谁能看到我做错了什么?

提前致谢,

克里夫

2 个答案:

答案 0 :(得分:2)

我想出了这个:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //...
    if(!loggedIn)
    {
        // Launch the app with login controller as the rootController
        self.window.rootViewController = loginController;

        // ...but switch to the original controller as soon as the UI is presented
        dispatch_async(dispatch_get_main_queue(), ^{
            self.window.rootViewController = originalRootController;

            // ...and silently present the login controller again with no noticeable changes
            [originalRootController presentViewController:loginController
                                                 animated:NO
                                               completion:NULL];
        });
    }

答案 1 :(得分:0)

Hpoe这个post会给你一些想法。