How to create custom events?
I have a view1 with a subview view2, where i have a UIButton button that has fires an action soSomething:
view1
--view2
----IBOutlet UIButton *button
-----(IBAction) doSomethingid)sender
clicking the button calls doSomething. now how can i dispatch a custom event in doSomething and catch it at the view1?
eg in view2:
Code:
-(IBAction)doSomething:(id)sender{
// Disptach the event for the parent "superView" to receive
}
then 开发者_开发百科in view1 have something that handles that event.
In your action event
// Dispatch the event for the parent "superView" to receive
-(IBAction) doSomething:(id)sender{
[[NSNotificationCenter defaultCenter] postNotification:@"SomeEventName"];
}
in your view1 viewdDidLoad
method write this code
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(methodToHandel) name:@"SomeEventName" object:nil];
and add this method to handle that event
-(void) methodToHandel{
// this method get call
}
You could use NSNotificatons or delegation depending on how your app is set up. I'd suggest looking at the docs to learn more about these things.
精彩评论