ios - 试图理解 Swift 中的协议(protocol)/委托(delegate)

标签 ios xcode delegates swift protocols

我是编程和 Swift 的新手,我正在尝试了解如何使用协议(protocol)和委托(delegate)在两个 View Controller (无 segue)之间传递数据。

我有一个 View Controller (VIEW A),它有一个文本字段和按钮。当用户点击该按钮时,它应该会在另一个 View Controller (VIEW B) 的标签中显示该文本。

我无法获得显示文本的标签 - 如果您能解释完成这项工作所需的条件,我将不胜感激。

非常感谢!

import UIKit

            protocol sendNameToViewB {

                func showNameLabel(name:String)
            }

            class ViewA: UIViewController {

                var delegate: sendNameToViewB?

                @IBOutlet weak var textField: UITextField!

                @IBAction func addButton(sender: AnyObject) {
                    delegate?.showNameLabel(textField.text)

                }
                 override func viewDidLoad() {
                    super.viewDidLoad()

                    // Do any additional setup after loading the view, typically from a nib.
                }
                override func didReceiveMemoryWarning() {
                    super.didReceiveMemoryWarning()
                    // Dispose of any resources that can be recreated.
                }


            }

            class ViewB: UIViewController, sendNameToViewB {

                @IBOutlet weak var theLabel: UILabel!

                func showNameLabel(name: String) {
                    theLabel.text = name
                }
            }

VIEW CONTROLLERS

最佳答案

首先,注意:您的 View Controller 名称应包含“ViewController”。有一个完全不同的类集合,它们继承自 UIView。将 View Controller 命名为 ViewA 会使您的类看起来只是一个 View 而不是 View Controller 。 View 位于应用程序的完全不同的层中。

现在,要将数据传递给另一个对象,您的首要要求是在它们之间有一个引用。可以在任一方向设置此引用。

一种可能是让 ViewControllerA 保留对 ViewControllerB 的引用。通过此引用,ViewControllerA 可以在按下按钮时调用 ViewControllerB 上的方法,该方法将您要传递的数据作为参数。

class ViewControllerA: UIViewController {
    @IBOutlet weak var viewControllerB: ViewControllerB!

    @IBAction func addButton(sender: AnyObject) {
        self.viewControllerB.showNameLabel(textField.text)
    }
}

另一种可能性是使用如您的标题所建议的委托(delegate)模式。这将涉及 ViewControllerB 对 ViewControllerA 的引用。最好不要直接了解 ViewControllerA 类,而是通过协议(protocol)。该协议(protocol)将定义一个方法,该方法返回您要“传递”给 ViewControllerB 的数据。这样,ViewContollerB 就可以在其“委托(delegate)”(恰好是 ViewControllerA)上调用协议(protocol)方法来获取所需的数据。

protocol ViewControllerBDelegate {
    func requiredText() -> String
}

class ViewControllerB: UIViewController {
    @IBOutlet weak var delegate: ViewControllerBDelegate?

    override func viewDidLoad() {
        if let actualDelegate = self.delegate {
            self.theLabel.text = actualDelegate.requiredText()
        }
    }
}

您选择哪种方法实际上取决于您在这种情况下的需要。委托(delegate)模式更好地让您的对象减少耦合,但如果您已经需要从 ViewControllerA“触发”ViewControllerB 上发生的事情,那么可能需要更直接的方法。

关于ios - 试图理解 Swift 中的协议(protocol)/委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25589240/

相关文章:

android - 在移动测试类中使用 System.getProperty ("os.name")不提供连接设备的 os.name

ios - UITableView 的圆上角

ios - Swift CoreData——无法将类型 'Int' 的值分配给类型 'Int16'

IOS:用手指画一条线

swift - 我如何使用协议(protocol)将数据从我的 View Controller 传递到其中的容器?

ios - 直接使用访问器的核心数据 NSTimeInterval 存在错误

ios - 在 Xcode 上的 2 个框架之间切换

iphone - 构建通用 iPad 应用程序 - 设备识别代码在哪里?

ios - Objective-C,委托(delegate)为适配器模式 : who is the adaptee?

objective-c - 如何将 MainWindow.xib 连接到空项目