让我们考虑一下下面的代码:
protocol A {
func doA()
}
extension A {
func registerForNotification() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidShow:"), name: UIKeyboardDidShowNotification, object: nil)
}
func keyboardDidShow(notification: NSNotification) {
}
}
现在来看一个实现A的UIViewController子类:
class AController: UIViewController, A {
override func viewDidLoad() {
super.viewDidLoad()
self.registerForNotification()
triggerKeyboard()
}
func triggerKeyboard() {
// Some code that make key board appear
}
func doA() {
}
}
但令人惊讶的是,这段代码崩溃时出现了一个错误:
keyboardDidShow:]:无法识别的选择器已发送到实例0x7fc97adc3c60
那么,我应该在视图控制器本身中实现观察者吗?它不能留在分机里吗?
以下是已经尝试过的东西。
让A成为一个类协议。将keyboardDidShow作为签名添加到协议本身。
protocol A:class {
func doA()
func keyboardDidShow(notification: NSNotification)
}
https://stackoverflow.com/questions/33022540
复制相似问题