Short Term Memory Between VCs in Swift

耗尽温柔 提交于 2019-12-25 09:59:38

问题


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)
        }
    }

回答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.



来源:https://stackoverflow.com/questions/34004636/short-term-memory-between-vcs-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!