使用applicationwillenterforeground作为密码屏幕

Mat*_*ers 6 iphone uiapplicationdelegate ios4

在iOS4之前,我的应用程序的初始视图控制器将检查viewWillAppear中的密码开/关设置变量,如果设置为on,则会显示一个模态密码屏幕,该屏幕将保持不变,直到输入正确的密码或按下Home按钮.

使用iOS4,如果我的应用程序已经在后台,我希望用户感到很舒服,如果他们要将手机交给某人使用,则应用程序中包含的数据不易被访问.

由于应用程序可以返回到任何屏幕(应用程序最后一次打开的屏幕),我想我将使用UIApplicationWillEnterForegroundNotification到处使用本地选择器(重复的enterPasscode方法),每个都有正确的视图控制器来推送基于本地屏幕,但必须有一个更好的方法.

我可能在这里有一个轻微的概念误解.有人可以提出另一种方法,或者推动我继续下一步.我可以将此作为共享方法,但仍然知道要推送正确的本地视图控制器吗?

谢谢

编辑/ UPDATE:

简短版本:它有效,但可能仍有更好的方式(任何帮助赞赏)...

我用视图控制器创建了一个标准单例.

PasscodeView.h包含:

UIViewController *viewController;
@property (nonatomic, retain) UIViewController *viewController;
Run Code Online (Sandbox Code Playgroud)

PasscodeView.m包含:

@synthesize viewController;
Run Code Online (Sandbox Code Playgroud)

我把它放在AppDelegate中:

-(void)applicationWillEnterForeground:(UIApplication*)application {

    PasscodeView *passcodeView = [PasscodeView sharedDataManager];
    UIViewController *currentController =  [passcodeView viewController];
    NSLog(@"%@", currentController);  //testing
    EnterPasscode *passcodeInput = [[EnterPasscode alloc] initWithNibName:@"Passcode" bundle:nil];
    [currentController presentModalViewController:passcodeInput animated:NO];
    [passcodeInput release];
}
Run Code Online (Sandbox Code Playgroud)

以及我所有viewDidLoad中的以下内容,当我进入每个屏幕时更新当前视图控制器(只有2行,但似乎还有更好的方法):

PasscodeView *passcodeView = [PasscodeView sharedSingleton];
passcodeView.viewController = [[self navigationController] visibleViewController];
Run Code Online (Sandbox Code Playgroud)

我希望有一种方法可以从applicationWillEnterForeground获得当前的视图控制器,但我找不到它 - 这里的任何帮助仍然很感激.

为了保持一致性,我更改了一行并添加了一行以获得导航栏以匹配应用程序的其余部分并包含标题.

UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:passcodeInput];
[currentController presentModalViewController: passcodeNavigationController animated:NO];
Run Code Online (Sandbox Code Playgroud)

Sea*_*ell 3

您可以通过实施将这种行为集中在您的应用程序委托中

-(void)applicationWillEnterForeground:(UIApplication*)application;
Run Code Online (Sandbox Code Playgroud)

您可以实现一个存储当前适当的模态视图控制器的单例,并在每个视图控制器的 viewWillAppear 中更新它。

编辑:我假设您已经有一系列想要显示的视图控制器。我怀疑你真的需要它。如果您调用了 PasscodeInputController,那么您的 applicationWillEnterForeground 将类似于:

-(void)applicationWillEnterForeground:(UIApplication*)application {
    UIViewController *currentController =  [window rootViewController];
    PasscodeInputController *passcodeInput = [[PasscodeInputController alloc] init];

    [currentController presentModalViewController:passcodeInput animated:NO];
    [passcodeInput release];
}
Run Code Online (Sandbox Code Playgroud)

我希望这能更直接地解决你的问题。