0

My storyboard controllers: https://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];

}
Community
  • 1
  • 1
pendolf
  • 308
  • 3
  • 10

1 Answers1

0

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

enter image description here

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

Raon
  • 1,266
  • 3
  • 12
  • 25
  • Thank you for answer. I think segues is only using for pushing data forward. – pendolf Jul 15 '14 at 09:32
  • you can also pass back – Raon Jul 15 '14 at 09:35
  • @ user3840005 if you change the address from editVC it will reflect in the object of MapViewController.address – Raon Jul 15 '14 at 09:36
  • Hmm. When I add push segue from editVC buttonOk to MenuVC, it broke all navigation bars. – pendolf Jul 15 '14 at 09:43
  • You dont need to perform a segue here. The thing you want to do is to go back to the previous view controller in the navigation stack right? So you have to pop the view controller and go back. You can either manually trigger the navigation backbutton.. or just pop the view controller. – Raon Jul 15 '14 at 09:54
  • I need to send text from UITextField on editVC to MenuVC cell.textLabel.text. Its not previous, its pre previous VC. – pendolf Jul 15 '14 at 10:00
  • @user3840005 still you can do this... You will have a flow of these VC's right? ok, lemme assume it as Menu>Map>Edit declare NSString *address for all of these 3 VC's then when you push MAP assign MAP.address=MENU.address then while moving from MAP to EDIT do the same EDIT.address=MAP.address,... now 3 of these address variables point to the same NSString object. so if you change MAP.address it will change EDIT.address,MENU.address now..in ViewDidAppear of MENU VC and EDIT VC method if you assign textfield.text=_address OR self.address you will have the new value in the textfield – Raon Jul 15 '14 at 11:03