美文网首页
Swift 通知的创建与使用

Swift 通知的创建与使用

作者: 不要虚度美好的时光 | 来源:发表于2022-04-22 00:04 被阅读0次

    Swift 通知的创建与使用

    创建通知中心
    设置监听方法
    设置通知的名字
            NotificationCenter.default.addObserver(self, selector: #selector(test), name: NSNotification.Name(rawValue:"isTest"), object: nil)
    
    实现通知监听方法
    @objc func test(nofi : Notification){
            let str = nofi.userInfo!["post"]
            print(String(describing: str!) + "this notifi")
        }
    
    点击发送通知进行
        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            NotificationCenter.default.post(name: NSNotification.Name("isTest"), object: self, userInfo: ["post":"NewTest"])
        }
    
    最后要记得移除通知
        deinit {
            /// 移除通知
            NotificationCenter.default.removeObserver(self)
        }
    

    相关文章

      网友评论

          本文标题:Swift 通知的创建与使用

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