ios - 将 Swift Delegate 设置为 "self"以外的 View Controller 的语法是什么?

标签 ios swift uiviewcontroller delegates protocols

我有一个简单的程序,其中包含一个导航 Controller 和创建用于在 3-5 个屏幕之间切换的序列。数据在主视图 Controller 中,我已经在主视图 Controller 和第二 View Controller 之间正确设置了委托(delegate)和协议(protocol)。我遇到的问题是我不确定如何在主视图和第三 View 之间设置适当的委托(delegate)。我的主要问题在于覆盖 prepareForSegue 函数。

因为程序一次流过一个屏幕(主要转至第二个 View ,第二个 View 转至第三个 View ,等等)我不确定应该在哪里以及如何使用此功能。我已将主视图设置为遵守第三 View Controller 协议(protocol),但我不确定如何将其设置为委托(delegate)。

例如,我知道如果我重写第二个 View Controller 中的 prepareForSegue 函数,我就不能使用“self”作为委托(delegate)。我想让主视图成为委托(delegate),但我不确定使非“ self ” View 成为委托(delegate)的语法是什么。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "2ndViewTo3rdView"{
        let thirdVC:ThirdViewController = segue.destinationViewController as ThirdViewController
        thirdVC.delegate = (????)
    }
}

我尝试了类似“navigationController?.topViewController”的方法,但出现以下错误:类型“UIViewController”不符合协议(protocol)“ThirdViewDelegate”

如能提供这方面的帮助,我们将不胜感激。

最佳答案

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

  • customVCOneA 可以呈现 customVCThree
    -或者-
  • customVCOneB 可以呈现 customVCTwo
    -然后-
  • customVCTwo 介绍 customVCThree
  • customVCThree 仅发送指令以清除 customVCOneB 上的表单数据。

对于 swift 5:

在自定义VCThree(发送指令或信息)

课外:

// 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?

类内部(您的应用需要的触发协议(protocol)方法):

    // 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

    }

关于ios - 将 Swift Delegate 设置为 "self"以外的 View Controller 的语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27221998/

相关文章:

ios - SKSpriteNode 不显示

iphone - 将工具栏添加到 UITableViewController

ios - 计算 UITableViewCell 的最佳图像大小

ios - 如何截取 pickerViewController 和 UIView 的屏幕截图?

ios - 删除所选 UIButton 中的突出显示

iphone - Push ViewController 带模态动画(水平翻转)

ios - 呈现具有透明背景的模态视图 Controller

iphone - 看到 malloc 分配大块内存 - 试图找出原因 (iPhone)

swift - 在 Swift 中枚举类成员

swift - 使用核心数据查找重复条目