ios - Swift 中 VC 之间的短期内存

标签 ios swift segue uitableview

简短的版本是:

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

详细信息,如果需要的话

我有一个 viewController,其中选择了一个表格单元格。然后将该单元格的内容拉出并传递到编辑器 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

现在,在第二个/编辑器 viewController 中,用户可以点击一个按钮,要求移动单元格。 “移动屏幕”是一个不同的/第三个 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)
        }
    }

最佳答案

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

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

看到 cellForRowAtIndexPath 方法中的 let task = taskList_Cntxt[indexPath.row] 行了吗?这就是您如何在给定的 indexPath 访问数据源数组中的对象。该对象(您称之为 task)应该传递给下游 View Controller 。

关于ios - Swift 中 VC 之间的短期内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34004636/

相关文章:

iOS审核流程,短信验证

ios - 将实时摄像机视频从 iOS (iPhone/iPad) 流式传输到远程 PC/服务器

swift - 定时器在停用后继续触发

swift - Dismiss ViewController 不释放内存

ios - 如何将 UISearchBar 文本字段默认占位符 "Search"更改为 "Enter city name..."

ios - 如何在选择器 View 条目旁边添加图像?

swift - 错误: Cannot find an overload for 'contains' that accepts an argument type in while loop

ios - 从 TableView 单元格到另一个 View Controller 的 Segue 转换仅在任意时间有效 - 为什么?

ios - 从父 UICollectionViewCell 中的 UICollectionViewCell 的按钮继续

ios - 将离线 map 嵌入 IOS 应用程序