cs.matyi
cs.matyi

Reputation: 1264

Is there a way To call a Child UIViewController method from Parent UIViewController?

I'm new in objective-c and my problem is that in (xcode) the Parent UIViewController is monitoring Ibeacon's and when the user go to the Child UIViewController the monitoring is still running and I need to call a Child VC method from the Parent. Like if the Actual Closest Beacon is changed, I want to alert the user that "Hey! You moved! etc.". Is there a proper way to do it? (Sorry for the bad english and if I'm not so specific)

Upvotes: 1

Views: 617

Answers (2)

oxigen
oxigen

Reputation: 6263

If your parent has only one child:

MyChildController* child = self.childViewControllers[0];
[child yourMethod];

Upvotes: 0

Shamas S
Shamas S

Reputation: 7549

There are several ways to do it.

  1. Your ParentViewController could keep pointer to your ChildViewController and call its function, which you would write in ChildViewController, something like showUserAlert. Once this is fired, you can show the alert.

  2. Have the ParentViewController fire an NSNotification from NSNotificationCenter. The child should listen to it and act accordingly.

  3. The Child should implement a delegate and ParentViewController should be set up for that delegate.

I would obviously go for the first one.

Upvotes: 2

Related Questions