zoukankan      html  css  js  c++  java
  • Swift 添加KVO

         
    1.添加监听
       lab1.addObserver(self, forKeyPath: "text", options: [.new, .old], context: nil)
    
    
    
    2. 监听
        override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            if let old = change?[NSKeyValueChangeKey.oldKey] {
              print("old = (old)") //
            }
                  
            if let new = change?[NSKeyValueChangeKey.newKey] {
              print("new = (new)") // Albert
            }
        }
    
    
    3.移除KVO
        deinit {
            lab1.removeObserver(self, forKeyPath: "text")
        }

    2.添加多个KVO

           lab1.addObserver(self, forKeyPath: "text", options: [.new, .old], context: nil)
            lab1.addObserver(self, forKeyPath: "frame", options: .new, context: nil)
    
    
    
        override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            if keyPath == "text"{
                print("text改变")
            }else{
                print("frame 改变")
            }
            if let old = change?[NSKeyValueChangeKey.oldKey] {
              print("old = (old)") //
            }
                  
            if let new = change?[NSKeyValueChangeKey.newKey] {
              print("new = (new)") // Albert
            }
        }
    
        deinit {
            lab1.removeObserver(self, forKeyPath: "text")
            lab1.removeObserver(self, forKeyPath: "frame")
        }
    

      

    3. 两个label 都监听了 text, frame

            lab1.addObserver(self, forKeyPath: "text", options: [.new, .old], context: nil)
            lab1.addObserver(self, forKeyPath: "frame", options: .new, context: nil)
            
            lab2.addObserver(self, forKeyPath: "text", options: [.new, .old], context: nil)
            lab2.addObserver(self, forKeyPath: "frame", options: .new, context: nil)
    
    
    
    
        override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            if lab1.isEqual(object){
                print("lab1 改变")
                if keyPath == "text"{
                    print("lab1 text改变")
                    if let old = change?[NSKeyValueChangeKey.oldKey] {
                        print("old = (old)") //
                      }
                            
                      if let new = change?[NSKeyValueChangeKey.newKey] {
                        print("new = (new)") // Albert
                      }
                }else{
                    print("lab1 frame 改变")
                    if let old = change?[NSKeyValueChangeKey.oldKey] {
                        print("old = (old)") //
                      }
                            
                      if let new = change?[NSKeyValueChangeKey.newKey] {
                        print("new = (new)") // Albert
                      }
                }
            }else{
                
                print("lab2 改变")
                if keyPath == "text"{
                    print("lab2 text改变")
                }else{
                    print("lab2 frame 改变")
                }
            }
        }
    
    
    
        deinit {
            lab1.removeObserver(self, forKeyPath: "text")
            lab1.removeObserver(self, forKeyPath: "frame")
            lab2.removeObserver(self, forKeyPath: "text")
            lab2.removeObserver(self, forKeyPath: "frame")
        }
    

      

  • 相关阅读:
    如何用Vault下载.Text 096的源代码
    新增QQ表情
    TortoiseCVS比WinCVS好用多了
    上周热点回顾(5.276.2)
    Couchbase的bug引起的缓存服务器CPU占用高
    云计算之路阿里云上:Linux内核bug引起的“黑色10秒钟”
    上周热点回顾(5.205.26)
    云计算之路阿里云上:拔云见日的那一刻,热泪盈眶
    云计算之路试用Azure:遭遇第一次故障
    上周热点回顾(5.135.19)
  • 原文地址:https://www.cnblogs.com/qingzZ/p/13746082.html
Copyright © 2011-2022 走看看