gpt4 book ai didi

ios - Swift:继承 ViewController 并添加目标?

转载 作者:行者123 更新时间:2023-11-28 06:45:59 26 4
gpt4 key购买 nike

我正在使用 swift 创建一个应用程序,我将一个 View Controller 子类化,在其中我添加了一个点击手势识别器和一个用于监听键盘出现的 NSNotification。我将 keyboardWillShow 的选择器放在我的基本 View Controller 中的一个函数中。然而,当我将我的 View Controller 子类化并且我有键盘显示时,我的应用程序以 NSException 终止,表示它找不到选择器。谁能解释为什么会这样以及我该如何解决?

这是我的基本 View Controller 中的功能:

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)
}

我没有覆盖我的子类中的任何东西。什么会被继承,什么不会?

在此先感谢您的帮助!

最佳答案

您的选择器声明需要参数,但您的函数不需要参数。

要么从你的选择器声明中删除 :

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow"), name: UIKeyboardWillShowNotification, object: nil)

或者改变你的功能

func keyboardWillShow(notification: NSNotification) {
self.view.frame.origin.y += CGFloat(keyboardHeight)
}

keyboardWillHide 做同样的事情。

关于ios - Swift:继承 ViewController 并添加目标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36347739/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com