First, a note: Your names for view controllers should include "ViewController" in the name. There is an entirely different collection of classes that inherit from UIView
. Naming a View Controller just ViewA
makes it look like your class is just a view instead of a view controller. 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. 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.
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. Preferably, this would not be with direct knowledge of the ViewControllerA class, but instead through a protocol. The protocol would define a method that returns the data you want to "pass" to ViewControllerB. That way, ViewContollerB can call the protocol method on its "delegate" (which would happen to be ViewControllerA) to get the data it needs.
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…