2012-11-09 22 views

cevap

9

herhangi bir sınıf UIApplicationWillEnterForegroundNotification kayıt ve buna göre tepki verebilir. Uygulama temsilcisi için ayrılmaz ve kaynak kodunun daha iyi ayrılmasına yardımcı olur. Eğer bir blok kullanabilirsiniz

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

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

Veya: ViewController senin doğrudan da yeniden veri işlevini diyebiliriz bir tableViewController ise

+0

UIApplicationWillEnter kullanıcısını kullanmadığım için örnek bir bağlantı var mı? ForegroundNotification? – CroiOS

+3

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

+0

Mükemmel, işe yarıyor. Çok teşekkür ederim. – CroiOS

0

Sen HomeViewController nesneye işaret ediyorum uygulama temsilci sınıfında bir mal beyanında olabilir. Daha sonra, update işlevinizi applicationWillEnterForeground'da arayabilirsiniz.

7

sizin HomeViewController

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

için böyle bir viewDidLoad yöntemi oluşturun:

[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification 
                object:nil 
                queue:[NSOperationQueue mainQueue] 
               usingBlock:^(NSNotification *note) { 
                [[self tableView] reloadData]; 
               }]; 
+0

Gözlemciyi dealloc üzerinde kaldırmayı unutmayın! – Cyrille

+0

gerçekten Cyrille! – Tieme

İlgili konular