【问题标题】:Passcode ViewController Presentation from Modal View模态视图中的密码 ViewController 演示
【发布时间】:2012-03-09 23:58:03
【问题描述】:

我正在我的 iPhone 应用程序中实现密码功能,它有一个 UITabBarController 作为根视图控制器。在大多数情况下,通过在应用程序进入后台时从 tabBarController 显示模态密码 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 以呈现视图?如果是这样,这是如何完成的?谢谢。

【问题讨论】:

    标签: objective-c ios uiviewcontroller


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢。我正在使用ARC,所以内存很好。为了清楚起见,将来我将发布带有保留/释放的代码。如果我想在现有模式视图之上显示模式密码视图怎么办?有什么快速的方法来获取对当前视图控制器的引用,不管是不是模态的?
    【解决方案2】:

    我通常做的是确保我拥有的任何可能呈现模态视图控制器的视图在发送UIApplicationWillResignActiveNotification 通知时关闭模态视图控制器,而在我的应用程序委托中,我完全设置了它喜欢你的。

    但需要注意的是,每当您关闭上述模态视图控制器时,您需要确保在显示密码视图控制器之前将 animated: 设置为 NO 以关闭它们。

    【讨论】:

      猜你喜欢
      • 2018-01-17
      • 1970-01-01
      • 2015-07-23
      • 2011-06-15
      • 2016-02-06
      • 2010-10-05
      • 2012-07-03
      • 1970-01-01
      相关资源
      最近更新 更多