gpt4 book ai didi

iphone - 我可以调用 ViewController 类中的什么方法来检查它何时被带到前台?

转载 作者:行者123 更新时间:2023-12-01 17:43:52 25 4
gpt4 key购买 nike

我可以调用 ViewController 类中的什么方法来检查它何时被带到前台?

例如,我正在查看我的应用程序上的一个页面,然后我决定关闭该应用程序并稍后再返回。当我回到它时,屏幕上出现了与我看到的相同的 View 。但是...一旦我打开应用程序,我就想切换到另一个 View 。

我怎样才能做到这一点?

目前正在尝试这个:

 - (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.");
}
}

最佳答案

注册您的 View Controller 以观察 UIApplicationWillEnterForegroundNotification :

1)内部 View Controller 的init方法:

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillEnterForeground:)
name:UIApplicationWillEnterForegroundNotification
object:nil];

2)内部 View Controller 的dealloc方法:
[[NSNotificationCenter defaultCenter] removeObserver:self];

3)另外,让你的 View Controller 实现这个方法:
- (void) applicationWillEnterForeground:(NSNotification*) notification
{
// This method will be called just before entering the foreground;
// Do your stuff here
}

如果 UIApplicationWillEnterForegroundNotification的时间不适合您,请查看 UIApplication 的所有可用通知这里:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html

关于iphone - 我可以调用 ViewController 类中的什么方法来检查它何时被带到前台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11845672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com