【问题标题】:Short Term Memory Between VCs in SwiftSwift 中 VC 之间的短期记忆
【发布时间】:2015-11-30 17:39:08
【问题描述】:

简短的版本是:

在模态展示和解雇另一个 VC 期间,一个 VC 变量的值是否保持不变?当第二个 VC 被解散时,原始 VC 的变量是否仍然等于它们最后的值?

详情(如果需要)

我有一个 viewController,其中选择了一个表格单元格。然后将该单元格的内容拉出并传递给编辑器视图控制器,如下所示:

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

现在,在第二个/编辑器视图控制器中,用户可以点击一个按钮,要求移动单元格。 “移动屏幕”是一个不同的/第三个 VC。我想知道的是,我可以解雇编辑并期望原始 VC 记住最后选择的单元格吗?

  • 如果它会记住,那么我假设我可以将它传递给第三个/移动 VC。
  • 如果原来的 VC 单元变量不能保存最后一个单元,我必须想办法让它记住!可能是全局变量?

追加编辑以显示 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)
        }
    }

【问题讨论】:

  • 考虑复制粘贴代码而不是发布屏幕截图。

标签: ios swift segue tableviewcell


【解决方案1】:

是的。视图控制器只是对象,因为它们的属性只有在执行更改它们的代码时才会更改。在您的特定情况下,VC 将其表视图保留为属性(并且强烈地作为其视图的子视图),并且表视图保留一组选定的索引路径。不过要小心,UITableViewController 的子类可以默认清除 viewWillAppear (see here) 上的选择。

还请注意,您选择了一种大多数人认为奇怪的方法来初始化prepareForSegue 中的editorVC。获取选定的索引路径很好,但是获取一个单元格(一个视图),然后配置该单元格,然后从该单元格的子视图中获取数据源值是非常迂回的。

看到let task = taskList_Cntxt[indexPath.row] 方法中的cellForRowAtIndexPath 行了吗?这就是您访问给定indexPath 的数据源数组中的对象的方式。该对象(你称之为task)应该被传递给下游视图控制器。

【讨论】:

  • 感谢 Danh,我发布了您的要求。我将尝试找出一种通过整个单元格的方法。我认为我这样做的方式是为了获得灵活性。编辑器接收来自多个表格的单元格,并且这些单元格是不同的自定义类型。通过只传递名称/描述,我认为我为编辑器保持简单。
  • :-) 不是整个单元格。它根本不应该涉及单元格(视图)。传递您用于配置单元格的整个 object。您可以通过使用 indexPath.row 对数据源数组进行索引来获取该对象。我确定您在 cellForRowAtIndexPath 方法中的某个地方执行此操作。
  • 哦。没有看到你发布了新代码:它是 -> task = taskList_Cntxt[indexPath.row] 将 task 传递给下一个 vc。
  • 另外,请按照@zneak 的建议。将文本代码粘贴到问题中,然后通过选择它并在格式化工具栏上选择“{}”来格式化它。
  • 再次编辑,用代码替换图像。谢谢你的帮助,我明天试试你的通过任务的技巧。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 2018-08-22
  • 2013-03-30
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
相关资源
最近更新 更多