delegate,通知,闭包
//2个界面传值
界面1
/传值
//需求2个类传值传值有通知,属性,delegate,闭包;
import UIKit
//遵守代理
class ViewController: UIViewController,TwoMessageDelegate {
let textlable = UILabel();
let TwoVc = TwoViewController();
override func viewDidLoad() {
super.viewDidLoad()
self.title = "首页";
textlable.frame = CGRect(x:50,y:100,width:self.view.frame.size.width-100,height:30);
textlable.textColor = UIColor.black;
textlable.textAlignment = NSTextAlignment.center;
textlable.backgroundColor = UIColor.red;
textlable.text = "我要这这段话传给另一个界面"
// textlable.frame = (x:100, y:100,witdh:self.view.frame.size.width,height:30);
self.view .addSubview(textlable);
let btn = UIButton();
btn.frame = CGRect(x:50,y:150,width:100,height:30);
btn.setTitle("跳转下一页", for: UIControlState.normal);
btn.setTitleColor(UIColor.blue, for: UIControlState.normal);
btn.addTarget(self, action:#selector(btnOnlick), for: UIControlEvents.touchUpInside);
self.view .addSubview(btn);
//闭包传值
self.TwovcClouse();
//通知传值
self.TwoVCNotiftion();
}
//点击事件
@objc func btnOnlick(){
//属性传值
TwoVc.string = self.textlable.text!;
//
TwoVc.delegate = self;
self.navigationController?.pushViewController(TwoVc, animated: true);
//静态跳转
// self.present(<#T##viewControllerToPresent: UIViewController##UIViewController#>, animated: <#T##Bool#>, completion: <#T##(() -> Void)?##(() -> Void)?##() -> Void#>)
}
//界面二代理实现
func getMessage(controller: TwoViewController, string: String) {
self.textlable.text = string;
}
//界面二闭包传值
func TwovcClouse(){
//闭包
TwoVc.twoVcClouse = {(_ text:String) ->Void in
print("闭包传过来的值为\(text)");
self.textlable.text = text;
}
}
func TwoVCNotiftion(){
let NotifTwoVCaiton = NSNotification.Name(rawValue:"TWoVc");
NotificationCenter.default.addObserver(self, selector: #selector(upDataChange(notif:)), name: NotifTwoVCaiton, object: nil);
}
//接收到后执行的方法
@objc func upDataChange(notif: NSNotification) {
guard let text: String = notif.object as! String? else { return }
self.textlable.text = text;
}
//移除通知
deinit {
NotificationCenter.default.removeObserver(self);
}
界面2
//反向传值:1用delegate,和闭包
import UIKit
//代理步骤1
protocol TwoMessageDelegate:NSObjectProtocol
{
//步骤2定义生命函数
func getMessage(controller:TwoViewController,string:String)
}
//闭包
typealias TwoViewControllerClosure = (_ text:String) ->Void
class TwoViewController: UIViewController {
var string:String = "";
//代理变量
var delegate:TwoMessageDelegate?;
//闭包变量
var twoVcClouse:TwoViewControllerClosure?;
//通知
let NotifMycation = NSNotification.Name(rawValue:"TWoVc");
override func viewDidLoad() {
super.viewDidLoad()
self.title = "界面二";
let textlable = UILabel();
textlable.frame = CGRect(x:50,y:100,width:self.view.frame.size.width-100,height:30);
textlable.textColor = UIColor.black;
textlable.textAlignment = NSTextAlignment.center;
textlable.backgroundColor = UIColor.red;
if string.isEmpty {
print("空")
textlable.text = "字符串为空";
}else{
textlable.text = self.string;
}
self.view .addSubview(textlable);
let btn = UIButton();
btn.frame = CGRect(x:50,y:150,width:100,height:30);
btn.setTitle("返回上一页", for: UIControlState.normal);
btn.setTitleColor(UIColor.blue, for: UIControlState.normal);
btn.addTarget(self, action:#selector(btnOnlick), for: UIControlEvents.touchUpInside);
self.view .addSubview(btn);
}
//传值给上一个界面用闭包和代理分别试试
@objc func btnOnlick(){
//代理写法
// self.TwoVCdeleage();
//闭包写法
//self.TwoVcClosure();
//通知
self.TwoVcNotiftiaction();
self.navigationController?.popViewController(animated: true);
//静态返回
//self.dismiss(animated: <#T##Bool#>, completion: <#T##(() -> Void)?##(() -> Void)?##() -> Void#>)
}
func TwoVCdeleage() {
if delegate != nil {
//传值
delegate?.getMessage(controller: self, string: "我是界面二传给你的值��");
}
}
func TwoVcClosure() {
if twoVcClouse != nil {
twoVcClouse!("我是用闭包传值的");
}
}
func TwoVcNotiftiaction(){
//单个值
NotificationCenter.default.post(name: NotifMycation, object:"我是通知给你传的值收到木")
//多个值用(name: NotifMycation, object: nil, userInfo: ["" : ""])
}