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

View Controller を登録して監視しますUIApplicationWillEnterForegroundNotification

1) ビュー コントローラーの init メソッド内:

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

2) ビュー コントローラーの 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

于 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 に答える