2010-08-08 63 views
6

的iOS4之前,我的应用程序的初始视图控制器将检查在viewWillAppear中,如果设置变量/关设定密码,呈现模式密码屏幕将呆在那里,直到输入正确的密码或主页按钮被按下。使用applicationwillenterforeground的密码屏幕

随着iOS4的,如果我的应用程序一直在后台,我想包含在数据中的应用是不容易接触,如果他们要交出自己的手机给别人使用的用户感到舒适。由于应用程序可以返回到任何屏幕(该应用程序最后一个屏幕),我想我会在本地选择器(重复的enterPasscode方法)遍布整个地方使用UIApplicationWillEnterForegroundNotification,每个屏幕都有正确的视图控制器基于本地屏幕推送,但必须有更好的方法。

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

感谢

编辑/ UPDATE:

短版:它的工作原理,但可能仍有可能是一个更好的方法(能够理解的任何帮助)...

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

PasscodeView.h包含:含

UIViewController *viewController; 
@property (nonatomic, retain) UIViewController *viewController; 

PasscodeView.m:

@synthesize viewController; 

我把这个在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]; 
} 

,在我所有的viewDidLoad以下,当我进入每个屏幕时更新当前视图控制器(只有2行,但似乎仍然存在)一个更好的方法):

PasscodeView *passcodeView = [PasscodeView sharedSingleton]; 
passcodeView.viewController = [[self navigationController] visibleViewController]; 

我希望有已经得到来自applicationWillEnterForeground当前视图控制器的方式,但我无法找到它 - 在这里任何帮助,还赞赏。

为了保持一致性,我换了一个线,并增加了专线,以获得导航栏,以匹配应用程序的其余部分,包括标题。

UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:passcodeInput]; 
[currentController presentModalViewController: passcodeNavigationController animated:NO]; 

回答

3

您可以通过实现

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

你可能实现存储当前适当的模式视图控制器单身,在每个视图控制器的viewWillAppear中更新它集中在你的应用程序委托此行为。

编辑:我假设你已经有一系列你想展示的视图控制器。我怀疑你真的需要它。如果你有一个叫,说PasscodeInputController,那么你的applicationWillEnterForeground看起来是这样的:

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

    [currentController presentModalViewController:passcodeInput animated:NO]; 
    [passcodeInput release]; 
} 

我希望地址你的问题更直接。

+0

这是我开始的地方,但不知道要为视图控制器放置什么 - 我会给这个想法然后试一试。 – 2010-08-08 18:02:39

+0

请参阅上面的编辑。 – 2010-08-08 18:23:09

+0

我从概念上理解你正在尝试的 - 在这里提供一个密码屏幕而不是在每个视图控制器 - 看起来它应该工作,但事实并非如此。完全按照原样复制代码(将PasscodeInputController更改为EnterPasscode),生成良好,没有错误,没有警告,但没有出现。我最初并不熟悉UIWindow的rootViewController属性,但在读完它之后,它看起来应该可以工作 - 尽管找不到任何其他示例。我试了一些其他的东西 - 尝试在那里使用navcontroller,尝试了子视图,makeKeyAndVisible但没有。 – 2010-08-09 03:29:25

1

重新进入前景时,应用程序的行为应该与第一次启动时的行为尽可能相似。考虑到这一点,你可以结合你的applicationDidFinishLaunching:applicationWillEnterForeground:,但考虑到一些视图可能已经加载。该代码是这样的:

id myState = (isReentering) ? [self validateState] : [self loadStateFromSave]; 
NSArray * keyForObjectToLoad = [self objectsToLoadForState:myState]; 
for(NSString * key in keyForObjectToLoad) 
    if(![self objectForKey:key]) [self loadObjectForKey:key]; 

根据您的应用程序的细节可能需要最初的工作,但它有一些好处:

  • 将确保启动和重新发起类似,因此用户体验不是“随机的”。
  • 您可以在后台更轻松地释放许多不需要的内存。
  • 从中心位置管理应用程序的状态会更容易。