1

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

2 回答 2

5

注册您的视图控制器以观察UIApplicationWillEnterForegroundNotification

1)内部视图控制器的init方法:

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

2)内部视图控制器的dealloc方法:

[[NSNotificationCenter defaultCenter] removeObserver:self];

3)另外,让你的视图控制器实现这个方法:

- (void) applicationWillEnterForeground:(NSNotification*) notification
{
    // This method will be called just before entering the foreground;
    // Do your stuff here
}

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

于 2012-08-07T12:23:26.143 回答
1

跳转到 ApplicationDelegate 文件,您会发现以下方法。

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

}

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

}

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

}

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

}

请记住,接收与应用程序状态相关的通知(如 willResignActive、didEnterBackground、willEnterForeground)的不是 viewController。ApplicationDelegate 对象将处理这些通知。所以,试着把你的逻辑放在上面的方法中。希望有帮助。如果没有,请使用我的答案下方的评论添加您的查询。

于 2012-08-07T12:18:28.403 回答