2010-08-17 41 views
0

我有哪裏我顯示當用戶完成他能回來前一頁的設置使用模式的看法刷新ParentViewController

[self presentmodalviewcontroller:tempcontroller animated:yes];

一個設置頁的iPhone應用程序。

[self.dismissmodalviewcontroller animated:YES]; 

現在我想在用戶從設置頁面返回時重新加載我的主頁面。我讀了一些我應該使用@protocol and delegate的地方。我已經瀏覽了關於這個主題的一些網上教程。但我無法做到這一點。我不知道@protocol and delegate,因爲我是iPhone新手。

請幫助我一些很好的教程。如果你能夠建議我有一步一步描述我的需求的鏈接,那將是非常有益的。

期待您的幫助....

在此先感謝

喜悅

回答

2

另一個更容易的選擇是使用NSNotificationCenter。看看這個

sending data to previous view in iphone

+0

NSNotification中心語義上將模態視圖與根源分離......這是沒有意義的,因爲它是一對一關係,一個是另一個關係的明確委託。 – DexterW 2010-08-17 14:15:44

0

喜悅,

假設您有另一個呈現一個模態的viewController - 稱它爲「根」。

的模式被稱爲「莫代爾」

「根」會說,

[self presentModalViewController:Modal]; 

那麼,如何根知道什麼時候辭退模態?做到這一點的最佳方式是爲此行爲制定一個「協議」。

在用於模態頭文件,就不會有這樣的代碼:

@protocol ModalViewDelegate <NSObject> 

-(void)modalViewControllerDidFinish:(UIViewController *)viewController; 

@end 

然後,應該有模態的一個實例變量:

id<ModalViewDelegate> delegate; 

與屬性:

@property (assign) id<ModalViewDelegate> delegate; 

這使得每次Modal發送消息給它的屬性'委託'時,我們知道它有方法 - (void)modalViewControllerDidFinish:

所以我們假設在Modal中有一個你想關閉它的按鈕。該按鈕只需要調用[delegate modalViewControllerDidFinish:self];

在爲根的頭文件,聲明類是這樣的:

@class Root : UIViewController <ModalViewDelegate> 

你實現該方法modalViewControllerDidFinish這樣的:

-(void)modalViewControllerDidFinish:(UIViewController *)controller { 
    // any action you need to take on controller 
    [self dismissModalViewControllerAnimated:YES]; 
} 

這是否合理?

+0

感謝Greelmo爲您的詳細答案。我已經實現了,但它給了我錯誤 - [UIView modalViewControllerDidFinish:]:無法識別的選擇器發送到實例0xd38280 不知道爲什麼? – Joy 2010-08-17 14:38:14

+0

你必須說Modal.delegate = root; 確保您在根上實施了該方法。做一些閱讀。這是蘋果推薦的解決方案。 – DexterW 2010-08-17 17:05:59