1

I'm trying to pass data from UITableview to another UITableview, I succeed to pass data when I press on cell but I couldn't pass data back when I press on back button of navigation bar; I've tried global variables to solve this problem but since I'm storing date of those variables in database it causes a lot of problems to me, because it enforces all stored reminders to save the same data, I could to do some tricks to solve this problem but we all know that global variable is bad practice and does't confirm to OOP neither MVC, the question is how to pass data back with using Prepare for segue function; note that the table view are static not dynamic.

and if your were asking what data I'm trying to pass is days of week, when Friday and Saturday are selected for example it sends back true values to previous view and if Friday and Saturday are deselected false values are sent back to previous view

so how can I solve this problem?

thanks in advance

William Kinaan
  • 28,059
  • 20
  • 85
  • 118
User
  • 123
  • 4
  • 13
  • When you tap the back button, the delegate method `viewWillDisapear()` is called. Implement it and pass data back in this method – AnthonyR May 26 '16 at 22:33
  • how exactly, sorry but I'm kinda new to iOS development, I know that `viewWillDisapear()` will be executed once that view is disappeared, but that question here is how exactly to pass data back – User May 26 '16 at 22:43
  • I will show you, give me a minute – AnthonyR May 26 '16 at 22:44

2 Answers2

7

You need to do four things:

First

You should have a custom protocol such as:

public protocol DataBackDelegate: class {
    func savePreferences (preferencesRestaurantsArray : [Bool], preferencesCusinesArray : [Bool])
}

as you see, I supposed that you want to send back two arrays, but you can edit that in order to send whatever data and datatypes you want.

Second

Your first view controller (or table view controller since table view controller is just a subclass of view controller) has to conform to your custom protocol, such as:

class MainTableViewController: UITableViewController, DataBackDelegate  

Third

In your second view controller, you need to have a variable for that protocol, such as:

 weak var delegate: DataBackDelegate?

and then you catch the back action and inside it you call your custom protocol function, such as:

self.delegate?.savePreferences(Preferences2ViewController.preferencesRestaurantsArray, preferencesCusinesArray: Preferences2ViewController.preferencesCusinesArray)

Fourth

In your first main controller, in the segue that fires the second view controller, you should set the deleage to self, such as:

destination.dataBackDelegate = self

where destination is the second view controller

William Kinaan
  • 28,059
  • 20
  • 85
  • 118
1

I don't have implement the tableviews, I just show you the concept.

VIEWCONTROLLER A (first UITableView) :

import UIKit

class vc1: UIViewController {

//Just to store days and the selected state (at start they should be unselected I presume)

var selectedDays : [String : Bool] = [
    "Monday":false,
    "Tuesday":false,
    "Wednesday":false,
    "Thursday":false,
    "Friday":false,
    "Saturday":false,
    "Sunday": false
]

override func viewDidLoad() {
    super.viewDidLoad()

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    //set this current view controller to the other UIViewController which will be pushed (vc2)
    //You will need it later when pass back data
    let secondViewController = segue.destinationViewController as! vc2
    secondViewController.previousVC = self
}

}

VIEWCONTROLLER B (your 2nd UITableView) :

class vc2: UIViewController {


var previousVC : ViewController

override func viewDidLoad() {
    super.viewDidLoad()
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(true)

    //Suppose that I selected the day of monday and the day thursday
    //get the datasource from the previousVC UIViewController and set the correct selected days
    previousVC.selectedDays["monday"] = true
    previousVC.selectedDays["thursday"] = true

    //Here your previousVC selectedDays dictionary variable contains now the new values 
    //So you can do all you want with it when you will come back on your previous viewcontroller

    /*
     var selectedDays : [String : Bool] = [
     "Monday":true,
     "Tuesday":false,
     "Wednesday":false,
     "Thursday":true,
     "Friday":false,
     "Saturday":false,
     "Sunday": false
     ]
     */
}

}

Is this what you wanted ?

AnthonyR
  • 3,485
  • 1
  • 18
  • 41