简体   繁体   English

试图了解Swift中的协议/委托

[英]Trying to understand protocol/delegates in Swift

I'm new to programming & Swift and I am trying to understand how to pass data between two view controllers (no segue) with protocols and delegates. 我是编程和Swift的新手,我正在尝试理解如何在两个视图控制器(没有segue)之间传递数据与协议和委托。

I have a View Controller (VIEW A) which has a text field and button. 我有一个视图控制器(视图A),它有一个文本字段和按钮。 When the user hits that button, it should then show that text in a label in another View Controller (VIEW B). 当用户点击该按钮时,它应该在另一个视图控制器(视图B)的​​标签中显示该文本。

I cannot get the label to show the text - I would appreciate an explanation of what is required to make this work. 我无法获得标签来显示文本 - 我希望能够解释为使这项工作成为必要。

Thanks so much! 非常感谢!

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

查看控制器

First, a note: Your names for view controllers should include "ViewController" in the name. 首先,注意:视图控制器的名称应在名称中包含“ViewController”。 There is an entirely different collection of classes that inherit from UIView . UIView继承的类集合完全不同。 Naming a View Controller just ViewA makes it look like your class is just a view instead of a view controller. 命名视图控制器只是ViewA使得它看起来像一个视图而不是视图控制器。 Views are in an entirely different layer of your application. 视图位于应用程序的完全不同的层中。

Now, to pass data to another object , your first requirement is to have a reference between them. 现在,要将数据传递给另一个对象 ,您的第一个要求是在它们之间有一个引用。 This reference can be setup in either direction. 可以在任一方向上设置此引用。

One possibility is to have ViewControllerA keep a reference to ViewControllerB. 一种可能性是让ViewControllerA保持对ViewControllerB的引用。 Through this reference, ViewControllerA can call a method on ViewControllerB when the button is pressed which takes that data you want to pass as an argument. 通过此引用,ViewControllerA可以在按下按钮时调用ViewControllerB上的方法,该方法将您想要传递的数据作为参数传递。

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

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

The other possibility, is to use a delegate pattern like your title suggests. 另一种可能性是使用像你的标题所暗示的委托模式。 This would involve ViewControllerB having a reference to ViewControllerA. 这将涉及ViewControllerB具有对ViewControllerA的引用。 Preferably, this would not be with direct knowledge of the ViewControllerA class, but instead through a protocol. 优选地,这不是直接了解ViewControllerA类,而是通过协议。 The protocol would define a method that returns the data you want to "pass" to ViewControllerB. 该协议将定义一个方法,该方法返回您想要“传递”到ViewControllerB的数据。 That way, ViewContollerB can call the protocol method on its "delegate" (which would happen to be ViewControllerA) to get the data it needs. 这样,ViewContollerB可以在其“委托”(恰好是ViewControllerA)上调用协议方法来获取所需的数据。

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()
        }
    }
}

Which method you choose really depends on what you need in this circumstance. 您选择哪种方法实际上取决于您在这种情况下的需求。 The delegate pattern is better to keep your objects less coupled together, but if you are already needing to "trigger" things to happen on ViewControllerB from ViewControllerA then the more direct method is probably required. 委托模式更好地保持您的对象不那么耦合在一起,但如果您已经需要从ViewControllerA“触发”ViewControllerB上发生的事情,那么可能需要更直接的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM