简体   繁体   中英

How to pass data back from multiple view controllers to root view controller using delegate? Ios 7

My storyboard controllers: http://i.stack.imgur.com/4LBEd.png

MenuViewController -> ChooseViewControler -> MapViewController and EditViewController.

I need to pass variable called address from MapViewController or EditViewController to MenuViewController. How i can implement this?

I try to use delegate, from this answer Passing Data between View Controllers but dont understand, how to tell MapViewController or EditViewController that MenuController is its delegate before we push its on nav stack.

I do this at EditVC and its worked:

- (IBAction)OkButton:(id)sender {

    NSString *address = addressInput.text;
    MenuTableViewController *menuVC =
    [self.navigationController.viewControllers objectAtIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [prevVC.tableView cellForRowAtIndexPath:indexPath];
    cell.textLabel.text = address;

    [self.navigationController popToViewController:menuVC animated:YES];

}

I guess you have the classes of these viewcontrollers as

MenuViewController.h, MenuViewController.m

ChooseViewControler.h, ChooseViewControler.m

MapViewController.h, MapViewController.m

EditViewController.h, EditViewController.m

There is this method called - PrepareForSegue called when you perform a segue either programatically(performSegue method) or by using push or model in navigation controller

implement this on your source ViewController (VC) ie, if you are passing from MapViewController to EditViewController , Implement this in the .m file of MapViewController .

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"toEditViewController"])
    {
        EditViewController *destinationVC=(EditViewController*)segue.destinationViewController;
        destinationVC.address=self.address;
    }

}

Note that you have to identify the segue which is called by its name give in the storyboard

在此处输入图片说明

There many be many more segues you need to prepare...you need to do all this in the same method.

identify the segue by name>prepare the methods

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