2011-10-24 35 views
1

我在我的iPhone应用程序中实现了一个Passcode功能,它有一个UITabBarController作为根视图控制器。我把一切都在大多数情况下工作的伟大,从tabBarController显示模式密码的ViewController当应用程序进入后台,像这样:当应用已经显示,当一个模式视图控制器Passode 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来呈现视图,而应该首先检查当前视图的内容,然后让它显示密码?如果是这样,这是如何完成的?谢谢。

回答

0

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

第二 - 您可以保留一个简单的BOOL变量,每次模态视图被呈现或解散时都会被更新。如果有模态视图,只需在applicationDidEnterBackground:方法中调用dismissModalViewController:animated:即可。

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

+0

谢谢。我正在使用ARC,所以内存很好。为了清晰起见,我将来会使用保留/释放来发布代码。 如果我想在现有的模态视图之上显示模态密码视图?任何快速获取当前视图控制器的引用的方式,无论它是否为模态? –

0

我通常会做的是确保任何视图可以呈现模态视图控制器,以便在发送UIApplicationWillResignActiveNotification通知时关闭模态视图控制器,而在我的应用程序委托中,我将其设置为完全像你的。

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