<span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"></span>
<span style="font-size:18px;">import UIKit
class ViewController: UIViewController,protocolVCDelegate {
// 协议文本框
@IBOutlet weak var protocolText: UITextField!
// 闭包文本框
@IBOutlet weak var closureText: UITextField!
// 通知文本框
@IBOutlet weak var notificationText: UITextField!
// 协议传值
@IBAction func protocolPassData(sender: UIButton) {
let protocolVC = ProtocolVC()
protocolVC.title = "协议传值界面"
// 定义协议,实现协议方法
protocolVC.delegate = self
self.presentViewController(protocolVC, animated: true, completion: nil)
}
// 实现ProtocolVC中定义的协议方法
func getTextValue(title: String) {
self.protocolText.text = title
}
// 闭包传值
@IBAction func closurePassData(sender: UIButton) {
let closureVC = ClosureVC()
closureVC.title = "闭包传值"
// 实现闭包,通过回调传值
closureVC.callBack = ({ (title:String) -> Void in
self.closureText.text = title
})
self.presentViewController(closureVC, animated: true, completion: nil)
}
// 通知传值
@IBAction func notificationPassData(sender: UIButton) {
let notificationVC = NotificationVC()
notificationVC.title = "通知传值"
self.presentViewController(notificationVC, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
// 接收通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: "notificationPD:", name: "notificationPassData", object: nil)
}
// 从通知函数中取值
func notificationPD(info:NSNotification) {
let text = info.userInfo!["text"] as? String
if let str = text {
self.notificationText.text = str
NSNotificationCenter.defaultCenter().removeObserver(self, name: "notificationPassData", object: nil)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">协议方式:</span>
<span style="font-size:18px;">
// 定义协议
protocol protocolVCDelegate {
func getTextValue(title:String)
}
class ProtocolVC: UIViewController {
var textFiled:UITextField?
var delegate:protocolVCDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
let screenWidth = self.view.frame.size.width
textFiled = UITextField(frame: CGRectMake( 10, 40, screenWidth - 20, 35))
textFiled?.borderStyle = UITextBorderStyle.RoundedRect
textFiled?.placeholder = "输入..."
textFiled?.becomeFirstResponder()
let btn = UIButton(frame: CGRectMake( 10, 80, screenWidth - 20, 35))
btn.setTitle("确定", forState: UIControlState.Normal)
btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
btn.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(textFiled!)
self.view.addSubview(btn)
}
// 确定
func save() {
self.delegate?.getTextValue(self.textFiled!.text!)
self.dismissViewControllerAnimated(true) { () -> Void in
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">
</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">闭包方式:</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">import UIKit
// 定义闭包
typealias ClosureCallBack = (title:String) -> Void
class ClosureVC: UIViewController {
var textFiled:UITextField?
// 定义闭包
var callBack:ClosureCallBack?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
let screenWidth = self.view.frame.size.width
textFiled = UITextField(frame: CGRectMake( 10, 40, screenWidth - 20, 35))
textFiled?.borderStyle = UITextBorderStyle.RoundedRect
textFiled?.placeholder = "输入..."
textFiled?.becomeFirstResponder()
let btn = UIButton(frame: CGRectMake( 10, 80, screenWidth - 20, 35))
btn.setTitle("确定", forState: UIControlState.Normal)
btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
btn.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(textFiled!)
self.view.addSubview(btn)
}
func save() {
// 通过闭包传值
self.callBack!(title: self.textFiled!.text!)
self.dismissViewControllerAnimated(true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">
</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">通知</span><span style="font-family: Arial, Helvetica, sans-serif; font-size: 18px;">方式</span><span style="font-family: Arial, Helvetica, sans-serif;">:</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">import UIKit
class NotificationVC: UIViewController {
var textFiled:UITextField?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.whiteColor()
let screenWidth = self.view.frame.size.width
textFiled = UITextField(frame: CGRectMake( 10, 40, screenWidth - 20, 35))
textFiled?.borderStyle = UITextBorderStyle.RoundedRect
textFiled?.placeholder = "输入..."
textFiled?.becomeFirstResponder()
let btn = UIButton(frame: CGRectMake( 10, 80, screenWidth - 20, 35))
btn.setTitle("确定", forState: UIControlState.Normal)
btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
btn.addTarget(self, action: "save", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(textFiled!)
self.view.addSubview(btn)
}
func save() {
let dataDic = NSMutableDictionary()
dataDic.setValue(self.textFiled?.text, forKey: "text")
// 注册通知
let notification = NSNotification(name: "notificationPassData", object: nil, userInfo: dataDic as [NSObject:AnyObject] )
// 发送通知
NSNotificationCenter.defaultCenter().postNotification(notification)
self.dismissViewControllerAnimated(true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
</span></span></span>