[英]Why Unrecognized selector sent to instance?
我正在嘗試為uiview功能制作一個方便的Binding鍵盤。 我無法克服這個錯誤
***由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:' - [Twitter.LoginVC handleKeyboard:]:無法識別的選擇器發送到實例0x7ffbf142e970'
class KeyboardService {
var constraint: NSLayoutConstraint!
var vc: UIViewController!
func bind(bottomConstraint: NSLayoutConstraint, vc: UIViewController) {
constraint = bottomConstraint
self.vc = vc
NotificationService.instance.addKeyboardObservers(onVC: vc, handleKeyboardSelector: #selector(self.handleKeyboard(_:))) // **CRASHES HERE**
}
@objc func handleKeyboard(_ notification: NSNotification) {
NotificationService.instance.handleKeyboard(notification: notification, bottomConstraint: constraint, vc: vc)
}
}
這是我的notificationService:
class NotificationService {
static let instance = NotificationService()
func addKeyboardObservers(onVC vc: UIViewController, handleKeyboardSelector: Selector) {
NotificationCenter.default.addObserver(vc, selector: handleKeyboardSelector, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(vc, selector: handleKeyboardSelector, name: UIResponder.keyboardWillHideNotification, object: nil)
}
}
編輯:
class KeyboardService {
var constraint: NSLayoutConstraint!
var vc: UIViewController!
func bind(bottomConstraint: NSLayoutConstraint, vc: UIViewController) {
constraint = bottomConstraint
self.vc = vc
NotificationService.instance.addKeyboardObservers(self, handleKeyboardSelector: #selector(self.handleKeyboard(_:)))
}
@objc func handleKeyboard(_ notification: NSNotification) {
NotificationService.instance.handleKeyboard(notification: notification, bottomConstraint: constraint, vc: vc)
}
}
編輯2:
class KeyboardService {
var constraint: NSLayoutConstraint!
var vc: UIViewController!
func bind(bottomConstraint: NSLayoutConstraint, vc: UIViewController) {
constraint = bottomConstraint
self.vc = vc
NotificationService.instance.addKeyboardObservers(self, handleKeyboardSelector: #selector(handleKeyboard(_:)))
}
@objc func handleKeyboard(_ notification: NSNotification) {
NotificationService.instance.handleKeyboard(notification: notification, bottomConstraint: constraint, vc: vc)
}
}
在vc的viewDidLoad()中:
KeyboardService().bind(bottomConstraint: loginBtnBackViewBottomConstraint, vc: self)
您正在嘗試將選擇器發送到vc
; 但它是一個UIViewController
實際上沒有一個名為handleKeyboard(_:)
。 您應該更改為您的注冊方法:
func addKeyboardObservers(_ observer: Any, handleKeyboardSelector: Selector) {
NotificationCenter.default.addObserver(observer, selector: handleKeyboardSelector, name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(observer, selector: handleKeyboardSelector, name: UIResponder.keyboardWillHideNotification, object: nil)
}
然后當你使用它時,你會做:
NotificationService.instance.addKeyboardObservers(self, handleKeyboardSelector: #selector(self.handleKeyboard(_:)))
正如你可以看到你現在告訴觸發的選擇self
,因為它實際上是self
是有方法。 作為一般規則:將選擇器發送到實例,因此該實例必須具有所選方法。
如果你真的想將選擇器發送到viewController
實例,你可以創建一個extension
,將方法添加到每個UIViewController
extension UIViewController {
@objc func handleKeyboard(_ notification: NSNotification) {
// do your stuff here
}
}
然后,在注冊時做:
NotificationService.instance.addKeyboardObservers(onVC: vc, handleKeyboardSelector: #selector(vc.handleKeyboard(_:))) // notice the vc.handleKeyboard instead of self.handleKeyboard
編輯嘗試在視圖控制器中保留鍵盤服務:
let keyboardService = KeyboardService()
override func viewDidLoad() {
super.viewDidLoad()
keyboardService.bind(bottomConstraint: loginBtnBackViewBottomConstraint, vc: self)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.