2012-03-03 126 views
1

我想知道如何將數據傳回給AppDelegate。比方說,我有這個計劃將數據傳遞給AppDelegate

AppDelegate - > RootViewController -> FirstViewController -> SecondViewController. 

的SecondViewController有當AppDelegate中applicationWillTerminate方法被調用應保存的數據。我知道我可以使用singleton在應用程序中共享數據,但我不喜歡這種方法。通知在這種情況下似乎不起作用。

你能推薦我什麼?

回答

4

這應該得到該委託。

AppDelegate * delegate =(AppDelegate*) [UIApplication sharedApplication].delegate 
[delegate callSomeMethod:someData]; 
+0

你能告訴我一些例子嗎? – OhDoh 2012-03-03 13:38:04

+0

現在已更新。 – madmik3 2012-03-03 13:59:00

+0

謝謝。我現在知道了。是否有可能將數據直接傳遞給applicationDidEnterBackground? – OhDoh 2012-03-03 14:03:28

1

其他建議。在您AppDelegate.h類,你可以創建一個類的方法如下所示:

+ (AppDelegate *)getAppDelegate; 

,然後在AppDelegate.m

+ (AppDelegate *)getAppDelegate 
{ 
    return (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
} 

現在,無論你想就像你可以訪問委託(有後導入AppDelagate.h)。

AppDelegate* sharedDelegate = [AppDelegate getAppDelegate]; 

更簡潔的方法可能是創建一個類別並將代碼放在那裏。

現在我的問題是:爲什麼你需要保存委託內的數據?也許你可以使用單身。欲瞭解更多信息,請參閱singletons-appdelegates-and-top-level

希望它有幫助。