喜悅,
假設您有另一個呈現一個模態的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];
}
這是否合理?
NSNotification中心語義上將模態視圖與根源分離......這是沒有意義的,因爲它是一對一關係,一個是另一個關係的明確委託。 – DexterW 2010-08-17 14:15:44