简体   繁体   English

斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel?

[英]Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview?

How can I pass data from child viewcontroller prepareForSegue to change Title of a cell of parent viewcontroller tableview? 如何从子ViewController的数据传递prepareForSegue更改父ViewController TableView的单元格的标题?

I have done it this way, but I don't think it's right: 我这样做是这样,但我认为这是不对的:

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

{
    if segue.identifier == "unwindToCreateTask" {
        let contact = contactList[self.tableView.indexPathForSelectedRow()!.row]

        let destinationVC = segue.destinationViewController as! CreateTaskVC

        destinationVC.tableView.reloadData()

        if let destinationCell = destinationVC.tableView.dequeueReusableCellWithIdentifier("assignToCell") as? UITableViewCell {
            destinationCell.textLabel?.text = contact.contactName
            destinationCell.detailTextLabel?.text = contact.contactEmail
        }
    }
}

Your child view controller should not know about the implementation of its parent. 您的子视图控制器不应了解其父视图的实现。 You are reaching into the table view of the parent from the child, but it would be more correct for the parent to access the required data from the child. 您正在从子级访问父级的表视图,但是父级从子级访问所需数据会更正确。 Unwind segues allow you to do this by defining an unwind method in the parent. Unwind segues允许您通过在父级中定义unwind方法来执行此操作。

In your parent , you define an @IBAction method that takes a single UIStoryboardSegue parameter. 在您的父级中 ,定义一个采用单个UIStoryboardSegue参数的@IBAction方法。 This is called on the parent at the start of the segue, and allows you to "pass back" any data to the parent in a way that prevents the need for delegation. 这是在segue的开始时在父级上调用的,它允许您以防止委派的方式将任何数据“传递”回父级。 A UIStoryboardSegue has a sourceViewController property, so in the case of an unwind segue, the child is the source. UIStoryboardSegue具有sourceViewController属性,因此在展开segue的情况下,子级是源。

@IBAction func unwindToParent(segue: UIStoryboardSegue) {
    if let childVC = segue.sourceViewController as? ChildVC {
        // update this VC (parent) using newly created data from child
    }
}

After defining the method, you create the segue in your storyboard by control-dragging from the triggering element in your child scene to the "exit" icon at the top of that scene. 定义方法后,您可以通过控制拖动从子场景中的触发元素到该场景顶部的“退出”图标,在情节提要中创建序列。 From the menu that appears, choose the unwind action you just created in the parent. 从出现的菜单中,选择您刚刚在父级中创建的展开动作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从模态呈现的 ViewController 传递到 swift 5 中的父 ViewController? - How to pass the data from modally presented ViewController to parent ViewController in swift 5? 如何在Swift 2.0中使用prepareForSegue将数据从PFTableViewCell传递到下一个ViewController? - How do I pass the data from PFTableViewCell to the next ViewController using prepareForSegue in swift 2.0? Swift-将数据从TableView传递到第三个ViewController - Swift - pass data from tableview to third viewcontroller 将数据从父 ViewController 传递到没有故事板的子 VC - Swift 5 - Pass data from a Parent ViewController to Child VC without Storyboards - Swift 5 TableView嵌套在CollectionViewCell中的VC,我如何将数据从TableView传递回ViewController - VC with TableView nested in CollectionViewCell, how can i pass data from TableView back to ViewController 如何将数据从视图控制器传递到另一个表视图? - How to pass data from a viewcontroller to another tableview? 我如何将数据从SlideTableTableViewController传递到ViewController中嵌入的TableView - How i pass Data from SlideOut TableViewController to TableView embedded in ViewController 如何将数据从 Swift ViewController 传递到 Obj-C ViewController? - How to pass data from a Swift ViewController to an Obj-C ViewController? 如何使用Swift 3将数据从ViewController A传递到另一个ViewController B - How to pass data from viewcontroller A to another viewcontroller B using swift 3 iOS swift prepareForSegue无法在两个ViewController之间传递数据 - iOS swift prepareForSegue cannot pass data between two viewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM