J Max
J Max

Reputation: 2381

calling an method in a view controller from a child view

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 ViewController. I tried:

[self.superview buttonPressedMethod];

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

Upvotes: 2

Views: 1557

Answers (3)

Abhishek
Abhishek

Reputation: 1702

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

Upvotes: 0

Arben Pnishi
Arben Pnishi

Reputation: 591

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];
}

Upvotes: 0

Caleb
Caleb

Reputation: 124997

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.

Upvotes: 1

Related Questions