2011-04-22 85 views
5

我有一個子視圖視圖2,在那裏我有有火災的動作soSomething一個UIButton按鈕廠景:如何創建自定義事件?

view1 
--view2 
----IBOutlet UIButton *button 
-----(IBAction) doSomethingid)sender 

點擊按鈕調用DoSomething的。 現在我該如何在doSomething中調度一個自定義事件並在view1中捕獲它?

例如,在視圖2:

代碼:

-(IBAction)doSomething:(id)sender{ 
    // Disptach the event for the parent "superView" to receive 

} 

然後在廠景有什麼事情,處理該事件。

回答

6

在你的動作事件

// Dispatch the event for the parent "superView" to receive 
-(IBAction) doSomething:(id)sender{ 
    [[NSNotificationCenter defaultCenter] postNotification:@"SomeEventName"]; 
} 
view1 viewdDidLoad方法

編寫代碼

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

,並添加這個方法來處理該事件

-(void) methodToHandel{ 
    // this method get call 
} 
0

根據您的應用程序的設置,您可以使用NSNotificatons或委派。我建議看看文檔以瞭解更多關於這些東西的信息。