Ios Swift:子类化ViewController并添加目标?
我正在用swift创建一个应用程序,我将视图控制器子类化,在其中我添加了一个点击手势识别器和一个监听键盘出现的NSNotification。我将键盘willshow的选择器放入基本视图控制器的函数中。然而,当我对视图控制器进行子类化并显示键盘时,我的应用程序终止时出现了一个NSException,表示它找不到选择器。有人能解释一下为什么会发生这种情况,以及我如何修复它吗 以下是我的基本视图控制器中的功能:Ios Swift:子类化ViewController并添加目标?,ios,swift,inheritance,Ios,Swift,Inheritance,我正在用swift创建一个应用程序,我将视图控制器子类化,在其中我添加了一个点击手势识别器和一个监听键盘出现的NSNotification。我将键盘willshow的选择器放入基本视图控制器的函数中。然而,当我对视图控制器进行子类化并显示键盘时,我的应用程序终止时出现了一个NSException,表示它找不到选择器。有人能解释一下为什么会发生这种情况,以及我如何修复它吗 以下是我的基本视图控制器中的功能: override func viewDidLoad() { super.viewD
override func viewDidLoad() {
super.viewDidLoad()
setNotificationListers()
setTapGestureRecognizer()
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func setNotificationListers() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
func setTapGestureRecognizer() {
let tapped = UITapGestureRecognizer(target: self, action: "closeKeyboard")
tapped.numberOfTapsRequired = 1
self.view.addGestureRecognizer(tapped)
}
func closeKeyboard() {
self.view.endEditing(true)
}
func keyboardWillShow() {
self.view.frame.origin.y += CGFloat(keyboardHeight)
}
func keyboardWillHide() {
self.view.frame.origin.y -= CGFloat(keyboardHeight)
}
我没有重写子类中的任何内容。什么会被继承,什么不会被继承
提前感谢您的帮助 请注意,
选择器
方法有一个:
,这意味着您的方法需要有一个参数。
因此,您应该将两种方法更改为:
func keyboardWillShow(notification: NSNotification) {
self.view.frame.origin.y += CGFloat(keyboardHeight)
}
func keyboardWillHide(notification: NSNotification) {
self.view.frame.origin.y -= CGFloat(keyboardHeight)
}
无论如何,xCode 7.3已经改变了实现
选择器的方式。您可以使用这个很棒的库来处理键盘向上推。使用非常简单。选择器声明需要参数,但函数不需要参数
从选择器声明中删除:
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow"), name: UIKeyboardWillShowNotification, object: nil)
或者改变你的功能
func keyboardWillShow(notification: NSNotification) {
self.view.frame.origin.y += CGFloat(keyboardHeight)
}
对键盘执行相同的操作将隐藏