我可以调用 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.");
}
}