6

重複の可能性:
コントローラーがバックグラウンドから再開したことを確認する方法は?

ユーザーがapplicationWillEnterForegroundに入った後にビューを更新する方法は?

たとえばHomeViewControllerのリコールを完了したい。

HomeViewController に更新機能があり、ユーザーが入力したときに更新機能を呼び出してテーブル データをリロードしたい。

4

3 に答える 3

9

どのクラスもに登録しUIApplicationWillEnterForegroundNotification、それに応じて反応することができます。これはアプリデリゲート専用ではなく、ソースコードをより適切に分離するのに役立ちます。

于 2012-11-09T14:30:49.977 に答える
8

HomeViewController に対して、このような viewDidLoad メソッドを作成します

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(yourUpdateMethodGoesHere:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
}

// Don't forget to remove the observer in your dealloc method. 
// Otherwise it will stay retained by the [NSNotificationCenter defaultCenter]...
- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

ViewController が tableViewController の場合、データのリロード関数を直接呼び出すこともできます。

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:[self tableView]
                                             selector:@selector(reloadData)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];

}
- (void) dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

または、ブロックを使用できます。

[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification
                                                  object:nil
                                                   queue:[NSOperationQueue mainQueue]
                                              usingBlock:^(NSNotification *note) {
                                                  [[self tableView] reloadData];
                                              }];
于 2012-11-09T14:40:11.517 に答える
0

HomeViewControllerオブジェクトを指すプロパティをアプリデリゲートクラスで宣言できます。次に、applicationWillEnterForegroundで更新関数を呼び出すことができます。

于 2012-11-09T14:30:45.873 に答える