简体   繁体   中英

How to communicate with different view controller?

I have a VC that presents another VC modally. This 2nd VC pushes to a 3rd VC. The 3rd VC dismisses itself and needs to call a method on the initial VC. I can't seem to get this working. Any advice?

 VC3 *aVC3 = [[self.navigationController.viewControllers objectAtIndex:0] parentViewController];
 aVC3 = self.name;
 [aVC3 addExercise];
 [self dismissView];

Use delegate for backward messsaging .

Refer simple-delegate-tutorial-for-ios-development . Also refer the-basics-of-protocols-and-delegates

If you have UINavigationController there is no need of delegate to communicate to any viewController in navigationController like:

ViewController *objViewController = (ViewController *)[self.navigationController.viewControllers objectAtIndex:0]; //need to which controller u would like to communicate
[objViewController yourMethodHere]; // your method here

Here is an alternate method:

Step 1: Create a FirstViewController Property in the SecondViewController.h

@interface SecondaryViewController : UIViewController{
}
@property (nonatomic, strong) FirstViewController *viewControllerOne;

Step 2: set that property to self when presenting the SecondViewController in FirstViewController.m

@implementation SecondaryViewController

-(void)functionThatPresentsSecondViewController{
    SecondViewController *viewControllerTwo = [[SecondViewController alloc] init];
    [viewControllerTwo setViewControllerOne: self];
    [self presentViewController:viewControllerTwo animated:NO completion:nil];
}

Step 3: You can now call FirstViewController functions from SecondViewController by using the viewControllerTwo.viewControllerOne property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM