iOS:在扩展程序(swift)中从NSNotificationCenter返回值

时间:2016-08-08 04:25:25

标签: ios swift nsnotificationcenter swift-extensions

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

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类中更新CGFloat值(keyboardheight)...我在视图控制器中怎么样?我不知道键盘什么时候出去以及如何更新我的价值。 有什么建议吗?

2 个答案:

答案 0 :(得分:0)

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

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

如果您的视图控制器也需要得到通知,那么还需要符合此协议。

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

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 ..

答案 1 :(得分:0)

您可以使用 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
    }
}