ユーザーがapplicationWillEnterForegroundに入った後にビューを更新する方法は?
たとえばHomeViewControllerのリコールを完了したい。
HomeViewController に更新機能があり、ユーザーが入力したときに更新機能を呼び出してテーブル データをリロードしたい。
ユーザーがapplicationWillEnterForegroundに入った後にビューを更新する方法は?
たとえばHomeViewControllerのリコールを完了したい。
HomeViewController に更新機能があり、ユーザーが入力したときに更新機能を呼び出してテーブル データをリロードしたい。
どのクラスもに登録しUIApplicationWillEnterForegroundNotification
、それに応じて反応することができます。これはアプリデリゲート専用ではなく、ソースコードをより適切に分離するのに役立ちます。
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];
}];
HomeViewControllerオブジェクトを指すプロパティをアプリデリゲートクラスで宣言できます。次に、applicationWillEnterForegroundで更新関数を呼び出すことができます。