2011-04-22 89 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或委派。我建议看看文档以了解更多关于这些东西的信息。