简体   繁体   中英

How Do I Pass Data from Third View Controller Back to First View Controller?

I currently have a navigation controller that contains one view controller and two table view controllers. When the user navigates to the second table view controller, they can press an '+' bar button item which modally presents another table view with a static cell that contains a text field. The user can enter text and press 'Done' to save it. When my delegate method is called, the view controller is dismissed and that user-entered string is appended to the array in the second view controller. The table view then displays that user-entered text in a table cell. The array has been passed up the stack via the prepareForSegue method. This is currently working because I can see the strings in the table view that the array was instantiated with in the first view controller.

My problem occurs when the user navigates back down the stack to any view controller prior to the second table view, which contains the user-entered strings displayed in cells. I have successfully been able to pass the data to the previous view controller, obviously, but after much research and testing, I am unable to successfully get the user-entered string to persist. When I navigate to previous view controllers and then back go the second table view, the user-entered data is gone, and the array contains only the initial data.

I understand that arrays in Swift are structures and the values are copied when passed up the stack. This makes sense as to why the user-entered data disappears when I navigate to a previous view controller. The previous view controller does not actually contain that user-entered data. Does this mean that I need to append the string to the array in the initial view controller instead of the array in the second table view controller? If that is the case, I am unsure of how to access the initial view controller from the third view controller.

All of the documents and tutorials that I have accessed are based on passing the data from the current view controller back to the previous view controller, which is the delegate that then dismisses the view controller that accepted the input.

How can I pass the user-entered string from the last view controller back to the initial view controller?

Actually, there are a lot of messaging mechanisms in Objective C, and in addition to Phillip's answer I can suggest you to use NSNotificationCenter by using which your can post notifications and catch them where needed. Posted notification can have custom object, string in your case, and name, for which you should add observer in order to receive that notification.

Registering for particular notification:

[[NSNotificationCenter defaultCenter] addObserver:{object_to_handle_notification} selector:@selector(notificationReceived:) name:@"my_notification" object:nil];

Posting notification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"my_notification" object:userEnteredString];

Handling notification:

- (void)notificationReceived:(NSNotification *)notification
{
    NSString *userEnteredString = notification.object;
    //do whatever you want with the user entered string
}

Also, you should remove observer for the notification when you are done. Hope this was helpful. Good Luck!

Create a class that is responsible for maintaining your data model. You can either make it a singleton or have an object of the class available through your application delegate (or some other option). When a controller wants information to be available to other parts of the app, it sends it to the data model for update. When a controller wants to display information, it gets it from the data model.

No more data-dependencies between controllers that have to figure out what to exchange.

What you can do is pass the firstViewController as a parameter to the ThirdViewController in the prepareForSegue method from the SecondViewController.

FirstViewController:

class FirstViewController: UIViewController {
  var a:String!

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      var destination=segue.destinationViewController as? SecondViewController
      destination?.firstViewCont=self
    }
}

SecondViewController:

class SecondViewController: UIViewController {
  var firstViewCont:FirstViewController!

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      var destination=segue.destinationViewController as? ThirdViewController
      destination?.FViewCont=self.firstViewCont
    }
}

ThirdViewController:

class ThirdViewController: UIViewController {
  var FViewCont:FirstViewController!

  @IBAction func textFieldEdited(sender: UITextField) {
         FViewCont.a!=sender.text
  }
}

This is one of the most impractical ways one would do it. I suggest that you learn about Navigation View Controllers . They work like a charm when doing something like this.

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