引言
闭包和协议传值一直是iOS App中开发中非常常见的使用,记录下,本人对闭包协议也不是很理解。还有的一种方法是通知,等忙完了再更新。
1. 通知
- 第一个控制器的内容是:
import UIKit
class TZViewController: UIViewController {
var Label = UILabel()
var btn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "通知"
self.view.backgroundColor = UIColor.white
configureUI()
print("通知完毕")
let notificationName = Notification.Name(rawValue: "DownloadImageNotification")
// 注册通知
NotificationCenter.default.addObserver(self,
selector:#selector(downloadImage(notification:)),
name: notificationName, object: nil)
}
func configureUI(){
btn.frame = CGRect(x: 100, y: 300, width: kScreenwidth - 200, height: 44)
btn.setTitle("确定", for: .normal)
btn.setTitleColor(UIColor.black, for: .normal)
btn.addTarget(self, action: Selector("BtnClick"), for: .touchUpInside)
self.view.addSubview(btn)
Label.frame = CGRect(x: 20, y: 64, width: kScreenwidth - 40, height: 44)
Label.font = UIFont.systemFont(ofSize: 15)
Label.text = "通知传值"
Label.textColor = UIColor.black
Label.textAlignment = .center
self.view.addSubview(Label)
}
func downloadImage(notification: Notification){
let userInfo = notification.userInfo as! [String: AnyObject]
Label.text = "姓名: \(userInfo["name"] as! String),年龄: \(userInfo["age"] as! String) "
}
func BtnClick(){
let vc = TZViewController1()
self.navigationController?.pushViewController(vc, animated: true)
}
deinit {
// 移除通知监听
NotificationCenter.default.removeObserver(self)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
- 第二个控制器的内容是:
import UIKit
class TZViewController1: UIViewController {
var btn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "传值"
self.view.backgroundColor = UIColor.white
configureUI()
print("发送通知")
let notificationName = Notification.Name(rawValue: "DownloadImageNotification")
let dic = ["name": "zengen","age": "24"]
// 发送通知
NotificationCenter.default.post(name: notificationName, object: nil, userInfo: dic)
}
func configureUI(){
btn.frame = CGRect(x: 100, y: 300, width: kScreenwidth - 200, height: 44)
btn.setTitle("确定", for: .normal)
btn.setTitleColor(UIColor.black, for: .normal)
btn.addTarget(self, action: Selector("BtnClick"), for: .touchUpInside)
self.view.addSubview(btn)
}
func BtnClick(){
self.navigationController?.popViewController(animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
2. 协议的反向传值(一定在第一个控制器写代理,并且实现代理方法)
- 第一个控制器的内容:
import UIKit
class XYViewController: UIViewController{
var btn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "协议"
self.view.backgroundColor = UIColor.white
configureUI()
// Do any additional setup after loading the view.
}
func configureUI(){
btn.frame = CGRect(x: 100, y: 300, width: kScreenwidth - 200, height: 44)
btn.setTitle("确定", for: UIControlState.normal)
btn.setTitleColor(UIColor.black, for:UIControlState.normal)
btn.addTarget(self, action: "bClick", for: UIControlEvents.touchUpInside)
self.view.addSubview(btn)
}
func bClick(){
let vc = XYViewController1()
vc.delegate_ze = self
self.navigationController?.pushViewController(vc, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension XYViewController: ZEdelegte{
func change(title: String) {
self.btn.setTitle(title, for: .normal)
}
func changeColor(Color: UIColor) {
self.btn.setTitleColor(Color, for: .normal)
}
}
- 第二个控制器的内容是:
import UIKit
//要创建一个协议
protocol ZEdelegte : NSObjectProtocol{
//在协议里面,声明方法
// 第一个,改变标题
func change(title:String)
// 第二个,改变颜色
func changeColor(Color:UIColor)
}
class XYViewController1: UIViewController {
// 创建一个准守协议的对象
var delegate_ze :ZEdelegte?
var btn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "传值"
self.view.backgroundColor = UIColor.white
configureUI()
}
func configureUI(){
btn.frame = CGRect(x: 100, y: 300, width: kScreenwidth - 200, height: 44)
btn.setTitle("返回", for: UIControlState.normal)
btn.setTitleColor(UIColor.black, for:UIControlState.normal)
btn.addTarget(self, action: "bClick", for: UIControlEvents.touchUpInside)
self.view.addSubview(btn)
}
func bClick(){
delegate_ze?.change(title: "zengen")
delegate_ze?.changeColor(Color: UIColor.red)
self.navigationController?.popViewController(animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
3. 闭包(Block)反向传值
- 第一个控制器的内容:
import UIKit
class BBViewController: UIViewController {
let btn = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "闭包"
self.view.backgroundColor = UIColor.white
configureUI()
}
func configureUI(){
btn.frame = CGRect(x: 100, y: 300, width: kScreenwidth - 200, height: 44)
btn.setTitle("确定", for: UIControlState.normal)
btn.setTitleColor(UIColor.black, for:UIControlState.normal)
btn.addTarget(self, action: "bClick", for: UIControlEvents.touchUpInside)
self.view.addSubview(btn)
}
func bClick(){
let vc = BBViewController1()
vc.block = { (_ title:String, _ color:UIColor) in
self.btn.setTitle(title, for: UIControlState.normal)
self.btn.setTitleColor(color, for:UIControlState.normal)
}
self.navigationController?.pushViewController(vc, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
- 第二个控制器是:
import UIKit
typealias valuesBlock = (_ title:String, _ color:UIColor)-> Void
class BBViewController1: UIViewController {
var block: valuesBlock?
let btn = UIButton()
var text = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
self.title = "传值"
self.view.backgroundColor = UIColor.white
configureUI()
}
func configureUI(){
text.frame = CGRect(x: 10, y: 200, width: kScreenwidth - 20, height: 44)
text.placeholder = "输入文字"
text.textAlignment = .left
text.layer.borderWidth = 1
text.layer.borderColor = UIColor.gray.cgColor //边框颜色
text.layer.cornerRadius = 6.0
text.font = UIFont.systemFont(ofSize: 15)
self.view.addSubview(text)
btn.frame = CGRect(x: 100, y: 300, width: kScreenwidth - 200, height: 44)
btn.setTitle("返回", for: UIControlState.normal)
btn.setTitleColor(UIColor.black, for:UIControlState.normal)
btn.addTarget(self, action: "bClick", for: UIControlEvents.touchUpInside)
self.view.addSubview(btn)
}
func bClick(){
if text.text == ""{
block!("zengen", UIColor.red)
}
else{
block!(text.text!, UIColor.red)
}
self.navigationController?.popViewController(animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
- 最后附上github的demo地址:https://github.com/zengen1993/Notification