注册观察者时viewDidLoad()崩溃

时间:2019-03-15 20:06:58

标签: ios swift nsnotificationcenter

我在viewDidLoad中发生了崩溃。我不知道为什么会这样。下面是崩溃跟踪和所有代码。

crash

class WriteTestimonialViewController: UIViewController, UITextViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addPurpleBackground()

        title = "Write Testimonial"

        testimonialTextView.clearTextView(text: "Type testimonial")

        // Setup keyboard event
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        // Dismiss keyboard
        self.hideKeyboardWhenTappedAround()
    }

    // Setup keyboard event
    @objc func keyboardWillShow(notification:NSNotification){
        var userInfo = notification.userInfo!
        var keyboardFrame:CGRect = (userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        keyboardFrame = self.view.convert(keyboardFrame, from: nil)

        var contentInset:UIEdgeInsets = self.scrollView.contentInset
        contentInset.bottom = keyboardFrame.size.height
        scrollView.contentInset = contentInset
    }

    @objc func keyboardWillHide(notification:NSNotification){

        let contentInset:UIEdgeInsets = UIEdgeInsets.zero
        scrollView.contentInset = contentInset
    }

 }

extension UIViewController {

    // Dismiss keyboard on touch
    func hideKeyboardWhenTappedAround() {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
}

func addPurpleBackground() {
    let imageViewBackground = UIImageView()
    imageViewBackground.image = UIImage(named: "bgimage")

    imageViewBackground.contentMode = .scaleAspectFill
    imageViewBackground.clipsToBounds = true
    imageViewBackground.translatesAutoresizingMaskIntoConstraints = false

    self.insertSubview(imageViewBackground, at: 0)

    self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "|[imageViewBackground]|",
                                                       options: [],
                                                       metrics: nil,
                                                       views: ["imageViewBackground": imageViewBackground]))
    self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[imageViewBackground]|",
                                                       options: [],
                                                       metrics: nil,
                                                       views: ["imageViewBackground": imageViewBackground]))
}

1 个答案:

答案 0 :(得分:0)

我认为最好在viewWillAppear / viewDidAppear中注册通知,只有在实际显示视图时,您才对通知感兴趣。如果将其保留在viewDidLoad中,有时可能会加载视图,但视图不会立即出现在屏幕上。

From iOS 9 you don't need anymore to manually remove observer.

  

在OS X 10.11和iOS 9.0 NSNotificationCenter和   NSDistributedNotificationCenter将不再发送通知到   可能被释放的注册观察员

相关问题