我有一个带导航控制器的简单程序,并创建了可在3-5个屏幕之间切换的seques。数据在主视图控制器中,我已在主视图控制器和第二视图控制器之间正确设置了委托和协议。我遇到的问题是我不确定如何在主视图和第三个视图之间设置适当的委托。我的主要问题是覆盖prepareForSegue函数。
因为程序一次流动一个屏幕(主要分段到第二个视图,第二个视图分段到第三个视图等)我不知道我应该在哪里以及如何使用这个功能。我已经设置了主视图以遵守第3个视图控制器协议,但我不确定如何将其设置为委托。
例如,我知道如果我在第二个视图控制器中覆盖prepareForSegue函数,我就不能使用" self"作为代表。我想让主视图成为委托人,但我不确定是什么语法可以使非 - " self"查看代表。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "2ndViewTo3rdView"{
let thirdVC:ThirdViewController = segue.destinationViewController as ThirdViewController
thirdVC.delegate = (????)
}
}
我尝试了" navigationController?.topViewController,但是我收到以下错误:输入' UIViewController'不符合协议' ThirdViewDelegate'
在这方面的任何帮助将不胜感激。
答案 0 :(得分:1)
我已将此解决方案用于模态呈现的VC,其中:
课堂外:
// Establish protocol for communicaiton back to parent view controller.
protocol ClearVCOneBProtocol {
// State the method that communicates back to grandparent view controller.
func clearVCOneB(/*Other Desired Parameters*/)
}
在类内部(不在任何函数中):
// Declare the delegate used to communicate with parent view controller.
var delegate: ClearVCOneBProtocol?
在类内部(您的应用所需的触发协议方法):
// As View Controller is about to disappear.
override func viewWillDisappear(_ animated: Bool) {
// Check "Restoration Identifier" (set inside Interface Builder or by code).
if presentingViewController?.restorationIdentifier == "customVCTwo" {
// Instruct the delegate to clear the form.
delegate?.clearVCOneB()
}
else {
// Do NOT instruct the delegate to clear the form.
}
}
课堂之外:
// Extend the View Controller with the protocol and delegate methods.
extension customVCOneB: ClearVCOneBProtocol {
// Conform to ClearVCOneBProtocol protocol.
func ClearVCOneBProtocol(/*Other Desired Parameters*/) {
// Call a function that has been declared inside customVCOneB and/or access parameters included in protocol.
resetForm()
} // End ClearVCOneBProtocol.
} // End extension.
实例化并显示customVCThree的地方:
// Create the View Controller.
let newVC = self.storyboard?.instantiateViewController(withIdentifier: customVCThree) as! customVCThreeViewController
// If customVCTwo was presented by customVCOne...
if presentingViewController?.restorationIdentifier == "customVCOneB" {
// Set customVCOneB as the delegate of customVCThree.
customVCThree.delegate = presentingViewController as! customVCOneBViewController
}
答案 1 :(得分:0)
你试过吗?
navigationController?.topViewController as? YourClassThatConformsToThirdViewDelegate
您需要将YourClassThatConformsToThirdViewDelegate
替换为您的相关课程。
关键是as? YourClassThatConformsToThirdViewDelegate
部分。 UIViewController
显然不符合ThirdViewDelegate
,所以我觉得施法可以解决问题。
答案 2 :(得分:0)
我遇到了类似的问题,为了其他人需要替代答案,请继续。我通过将接收的VC作为prepare(forSegue :)中的参数传递给变量来解决此问题。
if let secondVC = (segue.destination as? UINavigationController)?.topViewController as? CustomViewController {
secondVC.firstVC = self
}
然后在我的secondVC中,创建一个我想要有这样的委托的ThirdVC的实例:
let thirdVC = storyboard.instantiateViewController(withIdentifier: "thirdVC") as? ThirdViewController
thirdVC?.Delegate = firstVC.self as! Delegate
希望这对某人有帮助