我可以调用ViewController类中的哪个方法来检查它何时被带到前台?
例如,我在我的应用程序上查看一个页面,然后我决定关闭该应用程序并稍后再回到它.当我回到它时,屏幕上显示的是我所看到的相同视图.但是......一旦我打开应用程序,我想转到另一个视图.
我怎样才能做到这一点?
目前尝试这个:
- (void) applicationDidBecomeActive:(NSNotification*) notification
{
[self checkActivity];
// Do your stuff here
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
return self;
}
- (void)checkActivity{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"Checking if re-authentication required...");
if([[defaults objectForKey:@"shouldgotologin"] isEqualToString:@"yes"]){
NSLog(@"View Should go to login...performing segue");
[defaults setObject:@"no" forKey:@"shouldgotologin"];
[defaults synchronize];
[self performSegueWithIdentifier:@"backtologin" sender:self];
} else {
NSLog(@"Should go to login is not true.");
}
}
Run Code Online (Sandbox Code Playgroud)
注册您的视图控制器以观察UIApplicationWillEnterForegroundNotification:
1)内部视图控制器的init方法:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
Run Code Online (Sandbox Code Playgroud)
2)内部视图控制器的dealloc方法:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Run Code Online (Sandbox Code Playgroud)
3)此外,让您的视图控制器实现此方法:
- (void) applicationWillEnterForeground:(NSNotification*) notification
{
// This method will be called just before entering the foreground;
// Do your stuff here
}
Run Code Online (Sandbox Code Playgroud)
如果时间UIApplicationWillEnterForegroundNotification不适合您,请查看UIApplication此处的所有可用通知:http:
//developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html
| 归档时间: |
|
| 查看次数: |
622 次 |
| 最近记录: |