【发布时间】:2015-07-13 20:18:38
【问题描述】:
我这几天一直在研究父/子视图委托的概念,目前了解如何将数据从父级传递给子级。但是,现在,我想要父(主 VC)中的一个按钮来重新加载子 VC 中显示的数据。
我正在尝试委托在子 VC 的类中激活但在父导航控制器中激活的方法。这样当我按下按钮时,就会执行子 VC 中的委托方法;就我而言,该方法将是重新加载表。为什么在尝试建立这种简单的委托关系时会出现这么多错误?
我的父/容器视图当前正在将一个方法委托给孩子,所以我从孩子 -> 父母设置了它。但我想从父级-> 子级进行设置。我几乎有:
struct Constants {
static let embedSegue = "containerToCollectionView"
}
class ContainerViewController: UIViewController, CollectionViewControllerDelegate {
func giveMeData(collectionViewController: CollectionViewController) {
println("This data will be passed")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == Constants.embedSegue {
let childViewController = segue.destinationViewController as! CollectionViewController
childViewController.delegate = self
}
}
来自孩子:
protocol CollectionViewControllerDelegate {
func giveMeData(collectionViewController: CollectionViewController)
}
class CollectionViewController: UIViewController {
var delegate:CollectionViewControllerDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.delegate?.giveMeData(self)
// Do any additional setup after loading the view.
}
我认为我的麻烦在于我在 prepareforsegue 中声明了子委托,所以这是直截了当的,但现在我想要反向委托。如何设置它以便我可以使用父 VC 的子方法?
【问题讨论】:
标签: swift uitableview delegates