如何从 subview Controller prepareForSegue传递数据来更改父 View Controller 表格 View 的单元格标题?
我已经这样做了,但我认为这是不对的:
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
}
}
}
最佳答案
您的 subview Controller 不应该知道其父 View Controller 的实现。您正在从子级访问父级的 TableView ,但父级从子级访问所需的数据会更正确。展开转场允许您通过在父级中定义展开方法来实现此目的。
在父级中,定义一个采用单个 UIStoryboardSegue
参数的 @IBAction
方法。这在转场开始时在父级上调用,并允许您以无需委托(delegate)的方式将任何数据“传递回”父级。 UIStoryboardSegue
具有 sourceViewController
属性,因此在展开转场的情况下,子级就是源。
@IBAction func unwindToParent(segue: UIStoryboardSegue) {
if let childVC = segue.sourceViewController as? ChildVC {
// update this VC (parent) using newly created data from child
}
}
定义方法后,您可以通过按住 Control 键从子场景中的触发元素拖动到该场景顶部的“退出”图标,从而在 Storyboard中创建 Segue。从出现的菜单中,选择您刚刚在父级中创建的展开操作。
关于ios - Swift:如何从 subview Controller prepareForSegue传递数据来更改父 View Controller TableView 单元格的TextLabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31948211/