简体   繁体   中英

ios swift how to set a save button in the navigation bar

I have a MainViewController which is a table view controller, with custom cells where the user can select some data. There is a button on this MainViewController that goes to PreferencesViewController , which has two buttons on the navigation bar, one for back (it is wokring now good), and the other one for save (here is my problem)

This is a screenshot of the navitation bar in the PreferencesViewController : 在此处输入图片说明

I drag a segue from the save bar button back to the MainViewController .

my problem

when I hit save, the MainViewController appears, but without saving the values the the user has already selected. sounds like a new instance of this MainViewContoller is being created.

What am i missing here? what other approach i should have already used please?

You need to connect your save button to an IBAction, not a segue.

In that IBAction, do whatever you need to do to save your data, and then do a dismissViewController:animated: call yourself in code.

If you wanted to use a segue you wouldn't be able to (easily) save your data, and yes, it creates a new instance.

An alternative would be to connect your save button to an unwind segue, rolls back, or unwinds, one or more segues to get back to a previous view controller. To do that you'd have to put your save logic in prepareForSegue (with code to test which segue is being invoked and only save in response to the save segue.)

i found the solution myself using:

1- custom delegate:

2- self.navigationController?.popViewControllerAnimated(true)

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