Swift3.0 通知
来源:互联网 发布:巧克力哪一款好吃知乎 编辑:程序博客网 时间:2024/06/06 05:36
通知 –> 本文不做多个页面接收通知,只在两个页面之间发送和接收,多个页面类似
SecondViewController代码块
import UIKitclass SecondViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.white let btn = UIButton() btn.setTitle("返回", for: .normal) btn.frame = CGRect(x: 100, y: 100, width: 100, height: 100) btn.backgroundColor = UIColor.purple btn.addTarget(self, action: #selector(backBtnClick), for: .touchUpInside) self.view.addSubview(btn) } func backBtnClick() { // 发送通知 post NotificationCenter.default.post(name: NSNotification.Name(rawValue: "CallBackNotification"), object: self, userInfo: ["info": "second controller "]) self.navigationController?.popViewController(animated: true) }}
FirstViewController
import UIKitclass FirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = UIColor.white let btn = UIButton() btn.setTitle("跳转", for: .normal) btn.frame = CGRect(x: 100, y: 100, width: 100, height: 100) btn.backgroundColor = UIColor.purple btn.addTarget(self, action: #selector(pushBtnClick), for: .touchUpInside) self.view.addSubview(btn) // 接收通知 NotificationCenter.default.addObserver(self, selector: #selector(callbackNotification(notification:)), name: NSNotification.Name(rawValue: "CallBackNotification"), object: nil) } func pushBtnClick() { let secondVC = SecondViewController() self.navigationController?.pushViewController(secondVC, animated: true) } // 收到通知要执行方法 func callbackNotification(notification: Notification) { let userInfo = notification.userInfo as! [String: AnyObject] print(userInfo["info"] as AnyObject,".....") } // 移除通知监听 deinit { NotificationCenter.default.removeObserver(self) }}
好了,简单的通知就这么实现了
阅读全文
0 0
- Swift3.0 通知
- Swift3.0中的通知notification
- swift3 键盘通知事件
- iOS swift3.0 广播通知Broadcast notification 的使用
- Swift3.0
- swift3.0
- Swift3.0
- swift3.0 传值总结(属性、代理、闭包、通知)
- Swift中通知的使用方法(Swift3)
- Swift3 通知的一般使用流程
- swift3.1:获取用户授权显示通知(iOS 10)
- swift3.0的改变
- Swift3.0学习
- Swift3.0 初识
- swift3.0 GCD
- swift3.0 gcd 变化
- swift3.0变化
- Swift3.0 - 集合 - Set
- 五险一金查询相关
- 每天一个知识点:2017/9/9:简单工厂模式
- Fragment中onTouchEvent的实现
- 如何安装ionic
- 基于TCP的多人聊天工具
- Swift3.0 通知
- Activity处理返回结果
- js事件流
- HDU 1042 N!(高精度计算阶乘)
- 全面升级Android面试之webview面试题集
- Magento ver. 1.9.2.4 .htaccess配置301不起作用
- 1075-Incorrect table definition;there can be only one auto column and it must be defined as a key
- faster rcnn 训练与测试
- 经典java面试题(1)