注册观察者时在viewDidLoad()中崩溃

问题描述 投票:1回答:1

我在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]))
}
ios swift nsnotificationcenter
1个回答
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将不再向可能已解除分配的已注册观察者发送通知

© www.soinside.com 2019 - 2024. All rights reserved.