每次我收到 NSNotification 并执行相应的选择器时,都会调用我的 View Controller 的 deinit
函数!
这是我的 View Controller 的代码:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "initModels", name: "xyz.notification", object: nil)
}
func initModels(){
print("Received notification to init models")
}
deinit {
print("De-init view controller")
NSNotificationCenter.defaultCenter().removeObserver(self)
}
每个 View Controller 收到通知后,都会正常执行“initModels”,但随后会直接调用“deinit”! 以下是控制台上打印的内容:
Received notification to init models
De-init view controller
我检查了与多线程相关的所有内容。 通知在主线程上发送,并在主线程上执行。
最佳答案
天哪!
由于我不知道的原因,当选择器名称以“init”开头时,它会导致调用 View Controller 的 deinit
函数!
这是我尝试过的函数名称:
- initModels >> 调用 deinit
- init_models >> 调用 deinit
- initmodels >> 工作正常
- initializeModels >> 工作正常
关于ios - 收到 NSNotification 后执行选择器会调用我的 View Controller 的 deinit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34541322/