无法通过 Delegate/Protocol 访问 parent VC 中的 func()
Can't access to func() in parent VC via Delegate/Protocol
无法通过 delegate/protocol 从 child 访问 parent 视图控制器中的功能。 print()
不打印。
在 Child VC 我有这个:
protocol MyViewControllerDelegate: class {
func requestExpandedView()
}
class MyViewController: UIViewController {
weak var delegate: MyViewControllerDelegate?
...
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("\(indexPath) didSelectItemAt")
delegate?.requestExpandedView()
}
}
在 Parent VC 我有这个:
extension MessagesViewController: MyViewControllerDelegate {
func requestExpandedView() {
print("Done") // doesn't print anything
requestPresentationStyle(.expanded)
}
}
怎么了?
创建实例时需要设置MyViewController
的delegate
,即
let vc = self.storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
vc.delegate = self
无法通过 delegate/protocol 从 child 访问 parent 视图控制器中的功能。 print()
不打印。
在 Child VC 我有这个:
protocol MyViewControllerDelegate: class {
func requestExpandedView()
}
class MyViewController: UIViewController {
weak var delegate: MyViewControllerDelegate?
...
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print("\(indexPath) didSelectItemAt")
delegate?.requestExpandedView()
}
}
在 Parent VC 我有这个:
extension MessagesViewController: MyViewControllerDelegate {
func requestExpandedView() {
print("Done") // doesn't print anything
requestPresentationStyle(.expanded)
}
}
怎么了?
创建实例时需要设置MyViewController
的delegate
,即
let vc = self.storyboard?.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
vc.delegate = self