开发者

calling an method in a view controller from a child view

开发者 https://www.devze.com 2023-04-11 10:49 出处:网络
In my ViewController I have created a view which contains a button and a bunch of other elements.When the button is pressed I want to call a method from the parent ViewCont开发者_运维问答roller.I trie

In my ViewController I have created a view which contains a button and a bunch of other elements. When the button is pressed I want to call a method from the parent ViewCont开发者_运维问答roller. I tried:

[self.superview buttonPressedMethod];

But the superview is no the ViewController but UIView. Is there anyway to do this?


This is exactly what the target/action mechanism is for. Make the method in the view controller an IBAction. Set the view controller as the button's target, and set the method as its action. That should be all you need to do.


At ParentViewController.h 
-(void)ParentMethod;

At ParentViewController.m
-(IBAction)button:(id)sender {
       [self ParentMethod];
}

At IBAction of Play Button on child view controller do this:
-(IBAction)ChildMethod:(id)sender {
       ParentViewController *parent=self.parentViewController;
       [parent ParentMethod];
}


Better create delegate in view controller and set delegate in child view, so when something occur call delegate method according to it.

0

精彩评论

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

关注公众号