我有一个简单的程序与一个导航控制器和序列创建之间切换3-5个屏幕。数据在主视图控制器中,我已经正确地设置了主视图控制器和第二视图控制器之间的委托和协议。我遇到的问题是,我不确定如何在主视图和第三视图之间设置适当的代理。我的主要问题是覆盖了prepareForSegue函数。
因为程序一次显示一个屏幕(主视图切换到第二个视图,第二个视图切换到第三个视图,依此类推)我不确定我应该在哪里以及如何使用这个函数。我已经将主视图设置为遵循第三视图控制器协议,但我只是不确定如何将其设置为委托。
例如,我知道如果我覆盖了第二个视图控制器中的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’
在这方面的任何帮助都将不胜感激。
发布于 2019-12-17 21:41:46
我已经将此解决方案用于模态呈现的VC,其中:
-Or-
-Then-
对于Swift 5:
在customVCThree中(发送指令或信息)
在类之外:
// 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.
}
}在customVCOneB中(接收指令或信息)
在类之外:
// 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.在customVCTwo中( customVCOneB和customVCThree之间的中介)
无论在何处实例化和呈现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
}发布于 2014-12-01 13:09:11
你试过了吗?
navigationController?.topViewController as? YourClassThatConformsToThirdViewDelegate
您需要将YourClassThatConformsToThirdViewDelegate替换为本例中的相关类。
关键是as? YourClassThatConformsToThirdViewDelegate部件。UIViewController显然不符合ThirdViewDelegate,所以我觉得造型可以解决这个问题。
发布于 2018-08-05 04:22:34
我也有类似的问题,为了其他需要替代答案的人,来吧。我通过将接收到的VC作为准备(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希望这能帮助到某人
https://stackoverflow.com/questions/27221998
复制相似问题