在 swift 中处理委托时出错

Error while working with delegate in swift

我在 XCode 11 工作,我正在尝试在我的代码中使用委托。但是,它给了我一个错误:Unexpectedly found nil while implicitly unwrapping an Optional value

我以前使用过协议和委托,但从未遇到过这个问题。有人可以帮我弄清楚可能是什么问题吗?提前致谢。

这是我的代码:

class RedScreenVC: UIViewController {

    var delegate: NavDelegate!

    var redView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))

    var navigateButton: UIButton = {
        let button = UIButton(frame: CGRect(x: 200, y: 350, width: 150, height: 50))
        button.setTitle("Navigate", for: .normal)
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        button.backgroundColor = .blue
        return button
    }()

    @objc func buttonAction(){
        if self.redView.backgroundColor == .gray {
            self.redView.backgroundColor = .systemPink
        }
    }

    override func viewDidLoad() {
        navigateButton.layer.cornerRadius = 25
        redView.backgroundColor = UIColor.gray

        delegate.navigate(text: "Navigation Success", isShown: true)

        view.addSubview(redView)

        view.addSubview(navigateButton)
    }
}

下面是我创建的委托协议:

protocol NavDelegate {
    func navigate(text: String, isShown: Bool)
}

您需要从 prepare 函数中分配委托值。或者,如果您不想使用 storyboard/xib,您可以从 ViewController 中为初始化 RedScreenVC(self) 分配委托。

import UIKit

class ViewController: UIViewController, NavDelegate {
    func navigate(text: String, isShown: Bool) {
        print("text: \(text) isShown: \(isShown)")
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        let common = SHService()
        common.printMe(printString: "Hello World")
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if (segue.identifier == "RedScreenVC") {
            let RedScreenVC = segue.destination as? RedScreenVC
            RedScreenVC?.delegate = self
        }
    }

    @IBAction func nextPageButtonEventLustener(_ sender: Any) {
        performSegue(withIdentifier: "RedScreenVC", sender: sender)
    }
}



import UIKit

protocol NavDelegate {
    func navigate(text: String, isShown: Bool)
}
class RedScreenVC: UIViewController {

     weak var delegate: NavDelegate?

    var redView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))

    var navigateButton: UIButton = {
        let button = UIButton(frame: CGRect(x: 200, y: 350, width: 150, height: 50))
        button.setTitle("Navigate", for: .normal)
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        button.backgroundColor = .blue
        return button
    }()

    @objc func buttonAction(){
        if self.redView.backgroundColor == .gray {
            self.redView.backgroundColor = .systemPink
        }
      self.delegate.navigate(text:"", isShown: true)

    }

    override func viewDidLoad() {
        navigateButton.layer.cornerRadius = 25
        redView.backgroundColor = UIColor.gray

        delegate.navigate(text: "Navigation Success", isShown: true)

        view.addSubview(redView)

        view.addSubview(navigateButton)
    }

}