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.
精彩评论