2012-04-27 33 views
15

我覺得我在這裏失蹤了一招......如何在調用applicationDidBecomeActive時告訴活動視圖控制器?

我只是想打電話viewDidLoad中或viewDidAppear當前活動視圖控制器時applicationDidBecomeActive被調用,這樣我就可以重新設定動畫或什麼的,當應用程序啓動從背景再次起來。我的一些觀點不關心,但其他人真的需要知道。

我使用的故事板和我的應用程序委託文件具有標準功能 - 但所有與EMPTY機構。例如,didFinishLaunchingWithOptions只是返回YES,不會做任何其他事情。故事板自動完成我所想的一切。

那麼我怎樣才能從我空白的,無信息的應用程序委託中與當前的視圖控制器交談?

+0

有很好可能做到這一點更簡單的方法,但我認爲,如果你添加一個屬性到您的應用程序委託@property(強,非原子)的UIViewController * currentViewController它會工作。然後,每次加載視圖時,請回到委託來設置該屬性。然後在applicationWillResignActive中,將其保存到NSUserDefaults並在應用程序再次變爲活動狀態時檢查值? – geraldWilliam 2012-04-27 23:47:05

回答

20

我會推薦使用通知。

在應用程序委託的applicationdidBecomeActive方法把這個代碼:

[[NSNotificationCenter defaultCenter] postNotificationName:@"appDidBecomeActive" object:nil]; 

在你當前活動視圖控制器的init()方法訂閱通知。

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(updateStuff)   
              name:@"appDidBecomeActive" 
              object:nil]; 

實現你的控制器中的「updateStuff」的方法,你應該能夠做你想做的,當應用程序被激活。

+0

謝謝,這很好,似乎是這些建議中最神祕的方法。雖然我稍微關心正確地移除觀察者。我在模態中使用了一堆ViewController,所以它們被加載和卸載了很多。我在 - (void)viewDidUnload中調用了addObserver - (void)viewDidLoad removeObserver。似乎工作正常。我會進一步測試... – 2012-04-28 01:24:48

+0

不要忘記[[NSNotificationCenter defaultCenter] removeObserver:self name:@「appDidBecomeActive」object:nil]; in viewDidUnload – 2012-04-28 10:01:39

+2

不應該忘記在dealloc中刪除Observer,因爲viewDidUnload在所有的場景中都不會被調用 – 2012-04-28 12:35:28

0

而不是試圖跟蹤哪個ViewController是最新的,你可以從你的AppDelegate發送一個NSNotification並在你的ViewController中訂閱它。這樣視圖控制器就會跟蹤它是否需要調用viewDidAppear。

+0

是的,這是我已經走了。由於我的大部分控制器不需要註冊,因此具有總體意義。 – 2012-04-28 03:45:37

0

你的AppDelegate將有一個窗口屬性,該窗口將有一個rootViewController屬性。你可以在這裏找到你的viewController。

如果您使用TabBarController,rootviewcontroller將是tabbarcontroller,您可以調用tabbarcontroller的selectedViewController來獲取當前的viewController。

UIViewController *rootViewController = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; 
if ([rootViewController isKindOfClass:[UITabBarController Class]]) 
    rootViewController = ((UITabBarController *)rootViewController).selectedViewController; 
else if ([rootViewController isKindOfClass:[UINavigationController Class]]) 
    rootViewController = ((UINavigationController *)rootViewController).topViewController; 

[rootViewController viewDidAppear]; 

如果您有導航控制器,或模態的觀點更復雜的視圖層次,你可以在presentedViewController,或致電topViewController。

+0

這個窗口屬性不適合我,它給了我這個錯誤:使用未聲明的標識符'窗口'。你能否更新答案,因爲我真的需要知道屏幕上的哪個視圖,然後更新它。 – Hamid 2012-09-17 01:04:40

+0

@Rose查看更新 – 2012-09-28 22:02:02

+0

感謝您的更新,我改變了它,它引發了一個異常,如下所示:'無法識別的選擇器發送到實例':(對於這行代碼:'UIViewController * vc = tabbarController.selectedViewController;' – Hamid 2012-09-29 19:08:17

51

,而不是從您的應用程序委託發送通知的,操作系統會自動發送一個通知,你可以觀察到:

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(initSongInfo) 
              name:UIApplicationDidBecomeActiveNotification 
              object:nil]; 

,當然一定要停止之前或裏面你dealloc方法有時觀察,通過調用:

[[NSNotificationCenter defaultCenter] removeObserver:self 
               name:UIApplicationDidBecomeActiveNotification 
               object:nil]; 
+3

這是比布蘭登布羅傑斯基目前接受的答案更好的解決方案。 – MattyG 2014-03-15 02:01:29

+2

這是做這件事的正確方法 – nilloc 2015-02-13 05:33:58

1

斯威夫特版本:

您可以在viewDidLoad中添加此行

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(viewDidBecomeActive), name: UIApplicationDidBecomeActiveNotification, object: nil) 

func viewDidBecomeActive(){ 
    print("viewDidBecomeActive") 
} 
相關問題