NSNotification.Name.UIKeyboardWillShow崩溃-无法找到原因 [英] NSNotification.Name.UIKeyboardWillShow crash - Unable to find cause

查看:76
本文介绍了NSNotification.Name.UIKeyboardWillShow崩溃-无法找到原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  __ CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20 



所示的行号为154,即:

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

以下是我编写的包含以下内容的代码:

 重写功能viewWillAppear(_动画:Bool){
super.viewWillAppear(动画)
label_title.addObserver(self,forKeyPath: contentSize ,选项:.new,上下文:无)
NotificationCenter.default.addObserver(自身,选择器:#selector(keyboardShown),名称:NSNotification.Name.UIKeyboardWillShow,对象:无);
NotificationCenter.default.addObserver(自身,选择器:#selector(keyboardHide),名称:NSNotification.Name.UIKeyboardWillHide,对象:无);
}

覆盖func viewWillDisappear(_b动画:Bool){
super.viewWillDisappear(动画)
label_title.removeObserver(self,forKeyPath: contentSize)
NotificationCenter.default.removeObserver(自身,名称:NSNotification.Name.UIKeyboardWillShow,对象:无)
NotificationCenter.default.removeObserver(自身,名称:NSNotification.Name.UIKeyboardWillHide,对象:无)
}

func keyboardShown(notification:NSNotification){
let info = notification.userInfo!
让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,宽度:self.notesView.content.frame.size.width,高度:self.notesView.content.frame.size.height-keyboardFrame.size.height)
}

首先,这是相当随机的,但我从不了解。其次,我无法找到确切的原因。这是因为通知观察者还是因为notesView(不是nil)。

如建议的


Users of my app have reported a random crash. I have integrated CrashAnalytics which is giving the following details :

__CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20. 

The line number indicated is 154, which is :

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

Following is the code I have written which consist of this line :

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

Firstly, this is quite random and I never get it. Secondly, I am not able to find exact cause for it. Is this because of notification observer or because of notesView (which is not nil).
As suggested here, should I remove keyboard notification observer in deinit ?
Please guide me through this if someone has experienced this previously.

解决方案

Change signature of your function to this

@objc func keyboardShown(_ notification: Notification)

这篇关于NSNotification.Name.UIKeyboardWillShow崩溃-无法找到原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆