1.使用懶加載創(chuàng)建按鈕, 代碼的封裝
view.addSubview(btn);
fileprivate lazy var btn:UIButton = {
let btn = UIButton(type: .custom)
//2.定義frame
btn.frame = CGRect(x: 50, y: 50, width: 50, height: 50)
btn.backgroundColor=UIColor.green
btn .setTitle("這是按鈕", for: .normal)
btn .addTarget(self , action:#selector(chushihuaBtn), for: .touchUpInside)
return btn
}()
2.關于通知的使用
NSNotification
//通知名稱常量
let NotifyChatMsgRecv = NSNotification.Name(rawValue:"notifyChatMsgRecv")
//發(fā)送通知
NotificationCenter.default.post(name:NotifyChatMsgRecv, object: nil, userInfo: notification.userInfo)
//接受通知監(jiān)聽
NotificationCenter.default.addObserver(self, selector:#selector(didMsgRecv(notification:)),
name: NotifyChatMsgRecv, object: nil)
//通知處理函數(shù)
func didMsgRecv(notification:NSNotification){
print("didMsgRecv: \(notification.userInfo)")
}