2012-11-09 11 views
6

可能性の重複:ユーザーがapplicationWillEnterForegroundに入った後
How to tell when controller has resumed from background?applicationWillEnterForegroundに入った後に表示を更新するにはどうすればよいですか?

がどのようにビューをリフレッシュするには?

たとえば、HomeViewControllerなどのリコールを完了したいとします。

私はHomeViewControllerで更新機能を持っており、ユーザーが更新機能を呼び出してテーブルデータを再ロードするときに入力したいと思います。

答えて

9

いずれのクラスもUIApplicationWillEnterForegroundNotificationに登録し、それに応じて対応することができます。これは、アプリケーションデリゲートには予約されておらず、ソースコードの分離を助けます。

+0

私は決してユーザーUIApplicationWillEnter ForegroundNotification? – CroiOS

+3

http://stackoverflow.com/questions/3535907/how-to-tell-when-controller-has-resumed-from-background – Cyrille

+0

優れています。どうもありがとうございました。 – CroiOS

0

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

7

あなたのViewControllerがtableViewControllerであれば、あなたも直接、リロードデータ関数を呼び出すことができ

- (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]; 
} 

ごHomeViewControllerのために、このようなviewDidLoadメソッドを作成します。

- (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]; 
               }]; 
+0

deallocでオブザーバーを削除するのを忘れないでください! – Cyrille

+0

確かにCyrille! – Tieme

関連する問題