美文网首页
观察者模式

观察者模式

作者: 古月思吉 | 来源:发表于2017-11-28 15:06 被阅读0次

    一、有参数的观察者模式:

    <1>传递Object对象参数:

    0.观察者对象宏定义:

    let kAliPayNotificationName = Notification.Name("aliPayNotification")// 支付宝支付结果通知
    let kAliPaySuccessNotificationObject = "aliPaySuccess"  //支付成功
    let kAliPayFailNotificationObject = "aliPayFail"    //支付失败
    

    1.添加观察者 & 监听方法实现 & 移除观察者:

    //(1)添加支付宝支付结果观察者
    NotificationCenter.default.addObserver(self, selector: #selector(aliPayNotificationAction(notification:)), name: kAliPayNotificationName, object: nil)
    
    //(2)接收到通知的方法-支付宝支付结果
    func aliPayNotificationAction(notification:Notification) {
        guard notification.object != nil else {
            return
        }
        if let notificationObjectStr = notification.object! as? String {
            if (notificationObjectStr == kAliPaySuccessNotificationObject) {
                print("支付成功!")
                self.title = "支付成功"
                self.view.addSubview(self.paySuccessView)
                self.view.bringSubview(toFront: self.paySuccessView)
            } else if (notificationObjectStr == kAliPayFailNotificationObject) {
                print("支付失败!")
                self.title = "支付失败"
                self.view.addSubview(self.payFailView)
                self.view.bringSubview(toFront: self.payFailView)
            } else {
                return
            }
        }
    }
    
    //(3)移除观察者
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    

    3.发送通知:

    if resultStatus == "9000" {//支付成功
        NotificationCenter.default.post(name: kAliPayNotificationName, object: kAliPaySuccessNotificationObject)
    } else {//支付失败
        NotificationCenter.default.post(name: kAliPayNotificationName, object: kAliPayFailNotificationObject)
    }
    

    <2>传递userInfo参数:
    0.观察者对象宏定义:

    let kChineseAudioPlayPlayModelCheckNotice = Notification.Name("kChineseAudioPlayPlayModelCheckNotice")//音频播放方式切换的通知
    

    1.添加观察者 & 监听方法实现 & 移除观察者:

    //添加观察者
            NotificationCenter.default.addObserver(self, selector: #selector(sequenceTypeCheckNoticeAction(notification:)), name: kChineseAudioPlaySequenceTypeCheckNotice, object: nil)
    
    //MARK: - 观察者监听方法
        func sequenceTypeCheckNoticeAction(notification:Notification) {
            guard notification.userInfo != nil else {
                return
            }
            if let notificationUserInfoDict = notification.userInfo! as? [String: AnyObject] {
                if let value = notificationUserInfoDict["playMode"] as? Int {
                    self.sequenceType = AudioSequenceType(rawValue: value) ?? .order
                    self.tableView.mj_header.beginRefreshing()
                }
            }
            return
        }
    
    deinit {
            //移除观察者
            NotificationCenter.default.removeObserver(self)
        }
    

    3.发送通知:

    //发送通知
    NotificationCenter.default.post(name: kChineseAudioPlayPlayModelCheckNotice, object: nil, userInfo: ["playMode": playMode.rawValue])
    

    二、无参数的观察者模式:

    let kloginSuccessNotice = Notification.Name("BookRoomRefreshData")
    //发送通知
    NotificationCenter.default.post(name: kloginSuccessNotice, object: nil)
    //添加观察者
    NotificationCenter.default.addObserver(self, selector: #selector(loginSuccessAction), name: kloginSuccessNotice, object: nil)
    //在添加观察者VC,实现监测到通知后的动作方法
    func loginSuccessAction() {
            loadData()
        }
    //销毁观察者
    deinit {
            NotificationCenter.default.removeObserver(self)
        }
    

    三、添加系统通知的观察者:

    //添加系统通知的观察者
    NotificationCenter.default.addObserver(self, selector: #selector(didEnterBackground), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil )
    

    相关文章

      网友评论

          本文标题:观察者模式

          本文链接:https://www.haomeiwen.com/subject/stftbxtx.html