首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Swift Delegate设置为除"self“之外的视图控制器的语法是什么?

将Swift Delegate设置为除"self“之外的视图控制器的语法是什么?
EN

Stack Overflow用户
提问于 2014-12-01 13:04:51
回答 3查看 1.8K关注 0票数 0

我有一个简单的程序与一个导航控制器和序列创建之间切换3-5个屏幕。数据在主视图控制器中,我已经正确地设置了主视图控制器和第二视图控制器之间的委托和协议。我遇到的问题是,我不确定如何在主视图和第三视图之间设置适当的代理。我的主要问题是覆盖了prepareForSegue函数。

因为程序一次显示一个屏幕(主视图切换到第二个视图,第二个视图切换到第三个视图,依此类推)我不确定我应该在哪里以及如何使用这个函数。我已经将主视图设置为遵循第三视图控制器协议,但我只是不确定如何将其设置为委托。

例如,我知道如果我覆盖了第二个视图控制器中的prepareForSegue函数,我就不能使用"self“作为委托。我想让主视图成为委托,但我不确定让非“self”视图成为委托的语法是什么。

代码语言:javascript
复制
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "2ndViewTo3rdView"{
        let thirdVC:ThirdViewController = segue.destinationViewController as ThirdViewController
        thirdVC.delegate = (????)
    }
}

我尝试了类似"navigationController?.topViewController,但我得到以下错误:类型'UIViewController‘不符合协议'ThirdViewDelegate’

在这方面的任何帮助都将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2019-12-17 21:41:46

我已经将此解决方案用于模态呈现的VC,其中:

  • customVCOneA可能会呈现customVCThree

-Or-

  • customVCOneB可能会呈现customVCTwo

-Then-

  • customVCTwo仅在customVCOneB上显示customVCThree

  • customVCThree发送清除表单数据的指令。

对于Swift 5:

在customVCThree中(发送指令或信息)

在类之外:

代码语言:javascript
复制
// 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*/)

}

在类内部(不在任何函数中):

代码语言:javascript
复制
// Declare the delegate used to communicate with parent view controller.
var delegate: ClearVCOneBProtocol?

在类内部(根据您的应用程序的需要触发协议方法):

代码语言:javascript
复制
    // 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中(接收指令或信息)

在类之外:

代码语言:javascript
复制
// 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:

代码语言:javascript
复制
    // 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
EN

Stack Overflow用户

发布于 2014-12-01 13:09:11

你试过了吗?

navigationController?.topViewController as? YourClassThatConformsToThirdViewDelegate

您需要将YourClassThatConformsToThirdViewDelegate替换为本例中的相关类。

关键是as? YourClassThatConformsToThirdViewDelegate部件。UIViewController显然不符合ThirdViewDelegate,所以我觉得造型可以解决这个问题。

票数 0
EN

Stack Overflow用户

发布于 2018-08-05 04:22:34

我也有类似的问题,为了其他需要替代答案的人,来吧。我通过将接收到的VC作为准备(forSegue:)中的一个参数传递给一个变量来解决这个问题。

代码语言:javascript
复制
if let secondVC = (segue.destination as? UINavigationController)?.topViewController as? CustomViewController {
            secondVC.firstVC = self
        }

然后在我的secondVC中,创建一个我想要委托的thirdVC的实例,如下所示:

代码语言:javascript
复制
let thirdVC = storyboard.instantiateViewController(withIdentifier: "thirdVC") as? ThirdViewController
        thirdVC?.Delegate = firstVC.self as! Delegate

希望这能帮助到某人

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27221998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档