在下面的代码中,我有一个名为“SenderViewController”的
请问是什么触发了主
ViewController
,当点击按钮时会向主ViewController
传递一条消息。但我不完全了解的是,主ViewController
中的方法是如何知道何时监听这个消息。请问是什么触发了主
ViewController
中的方法?
SenderViewController:
import UIKit
protocol SenderViewControllerDelegate {
func messageData(data: AnyObject)
}
class SenderViewController: UIViewController {
@IBOutlet weak var inputMessage: UITextField!
var delegate: SenderViewControllerDelegate?
@IBAction func sendData(sender: AnyObject) {
/
if inputMessage.text != ""{
self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
self.delegate?.messageData(inputMessage.text!)
}
}
}
主视图控制器:
import UIKit
class ViewController: UIViewController, SenderViewControllerDelegate{
@IBOutlet weak var showData: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func goToView(sender: AnyObject) {
let pvc = storyboard?.instantiateViewControllerWithIdentifier("senderViewController") as! SenderViewController
pvc.delegate = self
self.presentViewController(pvc, animated:true, completion:nil)
}
// What triggers this method, how it know when to listen?
func messageData(data: AnyObject) {
self.showData.text = "\(data)"
}
}
非常感谢!
SenderViewController
中的func sendData
中调用self.delegate?.messageData
,正在执行它。 - iphonic