2014-07-23 8 views

Odpowiedz

16

Czy próbowałeś już UIApplicationWillEnterForegroundNotification?

Aplikacja wysyła również powiadomienie UIApplicationWillEnterForegroundNotification krótko przed wywołaniem applicationWillEnterForeground:, aby dać zainteresowanym obiektom szansę zareagowania na przejście.

Subskrybuj zgłoszenia:

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

Wdrożenie kod, który trzeba by nazwać:

- (void) yourUpdateMethodGoesHere:(NSNotification *) note { 
// code 
} 

Nie zapomnij wypisać:

[[NSNotificationCenter defaultCenter] removeObserver:self]; 
+0

można wysłać fragment kodu iam mylić – user3115014

+0

Sprawdź to jedno: http://stackoverflow.com/questions/2191594/send-and-receive-messages-through- nsnotificationcenter-in-objective-c –

7

Swift w wersji 3

override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    NotificationCenter.default.addObserver(self, 
              selector:#selector(applicationWillEnterForeground(_:)), 
              name:NSNotification.Name.UIApplicationWillEnterForeground, 
              object: nil) 
} 

override func viewWillDisappear(_ animated: Bool) { 
    super.viewWillDisappear(animated) 
    NotificationCenter.default.removeObserver(self) 
} 

func applicationWillEnterForeground(_ notification: NSNotification) { 
    .... 
} 

można również użyć NSNotification.Name.UIApplicationDidBecomeActive

Powiązane problemy