模态视图中的密码 ViewController 演示
我正在我的 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 来显示视图?如果是这样,这是如何完成的?谢谢。
I'm implementing a Passcode feature in my iPhone app which has a UITabBarController as a root view controller. I have everything working great in most situations, by displaying a modal Passcode ViewController from the tabBarController when the app goes into the background, like so:
- (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];
}
}
My problem comes when the app is already displaying a modal view controller when it enters the background. Then, no passcode view appears. What would be the correct way to do this? Instead of just sending the message to the tabBarController to present the view, should I be checking first to see what the current view is, then have that present the passcode? If so, how is this done? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先 - 您正在泄漏内存,因为您没有
释放
您的passcodeView
和导航控制器nav
。其次 - 您可以保留一个简单的
BOOL
变量,每当呈现或关闭模态视图时都会更新该变量。如果有模态视图,只需在applicationDidEnterBackground:
方法中调用dismissModalViewController:animated:
即可。您还可以使用
[self.navigationController.topViewController class]
检查最前面的视图控制器,但我发现这是不可靠的。First - you are leaking memory because you do not
release
yourpasscodeView
and navigation controllernav
.Second - you could keep a simple
BOOL
variable that is updated whenever a modal view is presented or dismissed. If there is a modal view, just calldismissModalViewController:animated:
in yourapplicationDidEnterBackground:
method.You could also check the frontmost view controller with
[self.navigationController.topViewController class]
, but I have found this to be unreliable.我通常做的是确保我拥有的任何可能呈现模态视图控制器的视图,以便在发送
UIApplicationWillResignActiveNotification
通知时关闭模态视图控制器,而在我的应用程序委托中,我设置它和你的一模一样。但需要注意的是,每当您关闭上述模态视图控制器时,您都需要确保在提供密码视图控制器之前将
animated:
设置为NO
来关闭它们。What I usually do is to ensure that any views I have that may present a modal view controller to dismiss the modal view controller whenever it is sent the
UIApplicationWillResignActiveNotification
notification, while over in my app delegate, I set it up exactly like yours.One caveat though, is that whenever you dismiss the said modal view controllers, you need to ensure that you dismiss them with
animated:
set toNO
before presenting your passcode view controller.