[英]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.