开发者

How to create custom events?

开发者 https://www.devze.com 2023-02-28 14:54 出处:网络
I have a view1 with a subview view2, where i have a UIButton button that has fires an action soSomething:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消