gpt4 book ai didi

ios - NSNotification.Name.UIKeyboardWillShow 崩溃 - 无法找到原因

转载 作者:搜寻专家 更新时间:2023-11-01 07:05:15 24 4
gpt4 key购买 nike

我的应用程序的用户报告了随机崩溃。我已经集成了 CrashAnalytics,它提供了以下详细信息:

__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20. 

enter image description here

显示的行号是154,也就是:

self.notesView.content.frame = CGRect(x: self.notesView.content.frame.origin.x, y: self.notesView.content.frame.origin.y, width: self.notesView.content.frame.size.width, height: self.notesView.content.frame.size.height - keyboardFrame.size.height). 

以下是我编写的代码,其中包含这一行:

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
label_title.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardShown), name:NSNotification.Name.UIKeyboardWillShow, object: nil);
NotificationCenter.default.addObserver(self, selector: #selector(keyboardHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil);
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
label_title.removeObserver(self, forKeyPath: "contentSize")
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}

func keyboardShown(notification: NSNotification) {
let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
self.notesView.content.frame = CGRect(x: self.notesView.content.frame.origin.x, y: self.notesView.content.frame.origin.y, width: self.notesView.content.frame.size.width, height: self.notesView.content.frame.size.height - keyboardFrame.size.height)
}

首先,这是非常随机的,我从来没有得到它。其次,我找不到确切的原因。这是因为通知观察者还是因为 notesView(不是零)。
按照建议here , 我应该在 deinit 中删除键盘通知观察器吗?
如果有人以前遇到过这种情况,请指导我解决这个问题。

最佳答案

将您的函数签名更改为此

@objc func keyboardShown(_ notification: Notification)

enter image description here

关于ios - NSNotification.Name.UIKeyboardWillShow 崩溃 - 无法找到原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48478802/

24 4 0