0

The short version is:

Do the values of a VC's variables stay in tact during the modal presentation and dismissal of another VC? When the second VC is dismissed, are the original VC's variables still equal to their last values?

Details, If Needed

I have a viewController where a table cell is selected. The contents of that cell are then pulled out and passed onto an editor viewController like this:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    //Segue to mainVC editor, for editing action
    if (segue.identifier == "modalToEditor") && passingEdit == true {
        //Assign selection to a variable 'currentCell'
        let indexPath = tableView.indexPathForSelectedRow;
        let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! CustomTableViewCell;

        //Set cell text into variables to pass to editor
        let cellNameForEdit = currentCell.nameLabel!.text
        let cellDescForEdit = currentCell.descLabel.text

        //Pass values to EditorView
        let editorVC = segue.destinationViewController as! EditorView;
        editorVC.namePassed = cellNameForEdit
        editorVC.descPassed = cellDescForEdit
        editorVC.indexOfTap = indexPath
        editorVC.currentListEntity = currentListEntity

Now, in that second/editor viewController, the user may tap a button asking to move the cell. The "move screen" is a different/third VC. What I want to know is, can I dismiss the editor and expect the original VC to remember the last cell chosen?

  • If it will remember, then I'd assume I could then pass it onto the the third/move VC.
  • If the original VC cell variable won't still hold that last cell, I'd have to figure out a way to make it remember! Global variable maybe?

Appending an edit to show the cellForRowAtIndexPath

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    //Setup variables
    let cellIdentifier = "BasicCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CustomTableViewCell

    //Make sure the row heights adjust properly
    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 80.0


    //Create normal cells except when last cell
    if indexPath.row < taskList_Cntxt.count {
        let task =  taskList_Cntxt[indexPath.row]
        //Create table cell with values from Core Data attribute lists
        cell.nameLabel!.text = task.valueForKey("name") as? String
        cell.descLabel!.text = task.valueForKey("desc") as? String

        //Related to running TaskActions: Empty block function passed from custom cell VC
        cell.doWork = {
            () -> Void in
            self.doStuff(cell)
        }
    }
Dave G
  • 12,042
  • 7
  • 57
  • 83

1 Answers1

1

Yes. View controllers are just objects insofar as their properties are changed only if code is executed that changes them. In your specific case, the VC keeps its table view as a property (and strongly as a subview of its view), and the table view keeps an array of selected index paths. Careful though, subclasses of UITableViewController can clear the selection by default on viewWillAppear (see here).

Also note that you've chosen what most would regard a strange approach to initializing the editorVC in prepareForSegue. Getting the selected index path is fine, but then getting a cell (a view), then configuring that cell, then getting datasource values from the cell's subviews is very roundabout.

See the line let task = taskList_Cntxt[indexPath.row] in your cellForRowAtIndexPath method? That is how you access an object in the datasource array at the given indexPath. That object (what you've called task) should be passed to the downstream view controller.

Community
  • 1
  • 1
danh
  • 62,181
  • 10
  • 95
  • 136
  • Thanks Danh, I posted what you requested. I will try to figure out a way to pass the entire cell. I think I did it the way I did for flexibility. The editor receives cells from more than one table and the cells are different custom types. By passing just the name/desc, I thought I was keeping it simple for the editor. – Dave G Nov 30 '15 at 18:16
  • :-) not the entire cell. It should not involve the cell (a view) at all. Pass the entire **object** you used to configure the cell. You get that object by indexing your datasource array with the indexPath.row. I'm sure you do this somewhere in your `cellForRowAtIndexPath` method. – danh Nov 30 '15 at 18:18
  • Oh. Didn't see you posted new code: there it is -> `task = taskList_Cntxt[indexPath.row]` Pass that `task` to the next vc. – danh Nov 30 '15 at 18:20
  • Also, please, as @zneak suggests. Paste textual code into the question, then format it by selected it and selecting "{}" on the formatting toolbar. – danh Nov 30 '15 at 18:21
  • Edited again, replaced images with code. Thank you for the help, I'll try out your tip on passing the task tomorrow. – Dave G Nov 30 '15 at 23:46