0

I have a controller (name : Parent.m) extending UIViewController to which I add another view (controller (name: Child.m) ) which has a table view in it.

On click of one of the rows on the table of Child.m, I want to invoke a method in the Parent.m class. I do not want to create a new instance of Parent.m.

Could somebody explain how I can accomplish this. Do I need to define a protocol between the two? If so can somebody explain me how.

i_raqz
  • 2,919
  • 10
  • 51
  • 87

1 Answers1

4

If you're using a UINavigationController, you can get your hands on the parent view controller by doing something like this in your child view controller.

ParentViewcontroller * parentView = (ParentViewcontroller *)[self.navigationController.viewControllers lastObject]; 

If you are not using a UINavigationController, you could use notifications (may be overkill). Or Key Value Observing from the parent into the child (i.e. change the display or value of something based on a change in the child view controller).

Or, probably the most straightforward thing to do here is set the parent to be a delegate target of the child.

1 )

In your ChildViewcontroller.h file, create a protocol which looks something like this:

@protocol ChildViewDelegate

- (void) doSomethingWith: (NSString *) thisString;

@end

2)

Create a delegate property in your ChildViewcontroller (which you'll set to be the parent view).

3)

In your ParentViewcontroller.h file, add "<ChildViewDelegate>" after the UIViewController @interface declaration... i.e.

@interface ParentViewController : UIViewController <ChildViewDelegate>

4)

Implement the "doSomethingWith:" method.

5)

And when you instantiate / create the Child Viewcontroller from the parent, don't forget to set the delegate to the parent.

6)

Lasty, when you want to send a message from the child to the parent, it could be as simple as:

[delegate doSomewthingWith: thisString];
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Could you please explain how do I use the protocol stuff? I am new to iOS. And yes, I dont have a uinav controller. – i_raqz Jun 05 '12 at 02:32
  • Thanks. I did this. But I how do I call the doSomethingWith method in the ChildViewController... when do a [self doSomethingWith:tstString]; in the ChildViewController, XCode complains (warning) instance method not found. And on executing the app crashes unrecognized selector sent to instance – i_raqz Jun 05 '12 at 02:55
  • Yep. By calling "`[self doSomething...]`" you're calling the selector on the child view (which I presume is the "self" where you are calling it from). Hence the crash (since the child view controller isn't where the protocol method is actually implemented). If you set the delegate to the parent view controller, you should do "`[delegate doSomething...]`" instead. I've updated my code steps to show this. – Michael Dautermann Jun 05 '12 at 02:58
  • I got it...Thanks.. You made this super easy and also cleared my delegation doubts as well... – i_raqz Jun 05 '12 at 03:02
  • great answer! [this answer more than it should but it will describe the delegate part ](http://stackoverflow.com/questions/6168919/how-do-i-set-up-a-simple-delegate-to-communicate-between-two-view-controllers) – LuAndre Feb 23 '16 at 15:57