iOS: return 来自扩展中 NSNotificationCenter 的值 (swift)

iOS: return value from NSNotificationCenter in an extension (swift)

在我的扩展程序中,我以这种方式放置了键盘通知控件

protocol KeyboardSpaceProtocol {
    func addObservers()
    func removeObservers()
    func keyboardWillShow(notification:NSNotification) -> CGFloat
}

extension UIView: KeyboardSpaceProtocol {

    func addObservers() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
    }

    func removeObservers() {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func keyboardWillShow(notification:NSNotification) -> CGFloat {
        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()
        return keyboardRectangle.height
    }
}

现在我想在我的 UIView class 中更新一个 CGFloat 值 (keyboardheight)...我怎样才能在我的视图控制器中?我不知道键盘何时熄灭以及如何更新我的价值。 有什么建议吗?

首先,通知回调方法一般不需要return任何值。在这种情况下,'keyboardWillShow' 的 return 值被忽略,通知中心不使用。它只是在发送通知时调用此方法。

要记录键盘的高度,您需要在您的视图class或视图控制器class中有一个符合此协议的成员变量。

如果你的视图控制器也需要得到通知,那也需要遵守这个协议。

例如在您的视图中 class 您可以像这样跟踪高度-

class MyView : UIView {
  var keyboardHeight : CGFloat = 0.0 

  //Override the version defined in UIView extension here. 
  override func keyboardWillShow(notification:NSNotification){
        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()
        keyboardHeight =  keyboardRectangle.height
  }

}

HTH..

您可以使用 objc_associatedObject 在 运行 时间添加属性。

我的示例使用 UIViewController 而不是 UIView 只是因为它对我来说更合乎逻辑。

import Foundation
import UIKit
import ObjectiveC

protocol KeyboardSpaceProtocol {
    func addObservers()
    func removeObservers()
    func keyboardWillShow(notification:NSNotification) -> CGFloat
    var keyBoardHeight: CGFloat {get set}
}

private var keyBoardKey: UInt8 = 0
extension  UIViewController: KeyboardSpaceProtocol {

    var keyBoardHeight: CGFloat {
        get {
            return objc_getAssociatedObject(self, &keyBoardKey) as! CGFloat
        }
        set { objc_setAssociatedObject(self, &keyBoardKey, newValue,  objc_AssociationPolicy(rawValue: 0)!) }
    }

    func addObservers() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(UIViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
    }

    func removeObservers() {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func keyboardWillShow(notification:NSNotification) -> CGFloat {
        let userInfo:NSDictionary = notification.userInfo!
        let keyboardFrame:NSValue = userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
        let keyboardRectangle = keyboardFrame.CGRectValue()

        var selfAsProtocol = self as KeyboardSpaceProtocol
        selfAsProtocol.keyBoardHeight = keyboardRectangle.height

        return keyboardRectangle.height
    }
}