Modal View中的密码ViewController演示

时间:2011-10-24 18:49:30

标签: objective-c ios uiviewcontroller

我在我的iPhone应用程序中实现了一个密码功能,它有一个UITabBarController作为根视图控制器。在大多数情况下,通过在应用程序进入后台时从tabBarController显示模态Passcode ViewController,我的一切都运行得很好,如下所示:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    if ([[NSUserDefaults standardUserDefaults] valueForKey:kPasscodeStringKey]) {

        PasscodeEntryVC *passcodeView = [[PasscodeEntryVC alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:passcodeView];
        [tabBarController presentModalViewController:nav animated:NO];
    }
}

当应用程序在进入后台时已经显示模态视图控制器时出现问题。然后,不会出现密码视图。这样做的正确方法是什么?我应该首先检查当前视图是什么,而不是仅仅将消息发送到tabBarController来呈现视图,然后让它显示密码?如果是这样,这是怎么做到的?感谢。

2 个答案:

答案 0 :(得分:0)

首先 - 您正在泄漏记忆,因为您没有release passcodeView和导航控制器nav

第二 - 您可以保留一个简单的BOOL变量,只要呈现或取消模态视图,该变量就会更新。如果有模态视图,只需在dismissModalViewController:animated:方法中调用applicationDidEnterBackground:

您还可以使用[self.navigationController.topViewController class]检查最前面的视图控制器,但我发现这是不可靠的。

答案 1 :(得分:0)

我通常做的是确保我拥有的任何视图可能会提供模态视图控制器,以便在发送UIApplicationWillResignActiveNotification通知时关闭模态视图控制器,而在我的应用程序委托中,我设置它和你的完全一样。

但有一点需要注意的是,无论何时解除所述模态视图控制器,都需要确保在呈现密码视图控制器之前将animated:设置为NO来关闭它们。

相关问题