1

我正在尝试在单个故事板中的几个视图控制器之间进行简单的导航(使用协调器)。 在此处输入图像描述

我遇到的错误是 AuthenticateErrorDelegate 的代表返回nil,这就是为什么我不能 @jump@ 到父级 - 登录协调器来处理我的逻辑

代码:

import UIKit

protocol AuthenticateErrorDelegate: class {
    func jump()
}

class AuthenticateErrorViewController: UIViewController {

    weak var delegate: AuthenticateErrorDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    @IBAction func backToLogin(_ sender: Any) {

        self.delegate?.jump(). //delegate return NIL %(
        print("jump to AUTH VC")

    }
}

enter code here协调员:

import UIKit

protocol LoginCoordinatorDelegate: class {
    func didFinishLogin() 
    func didErrorLogin()
    func openAuthForm()
}

class LoginCoordinator: NSObject {

    var navigationController: UINavigationController
    var childCoordinators: [NSObject] = []
    private var authenticateViewController: AuthenticateViewController
    private var authenticateErrorViewController: AuthenticateErrorViewController

    weak var delegate: LoginCoordinatorDelegate?

    deinit {
        print("Deallocating \(self.description)")
    }

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController

        let storyboard = UIStoryboard(name: "Login", bundle: nil)

        let vc = storyboard.instantiateViewController(withIdentifier: "AuthenticateViewController") as! AuthenticateViewController
        self.authenticateViewController = vc

        let evc = storyboard.instantiateViewController(withIdentifier: "AuthenticateErrorViewController") as! AuthenticateErrorViewController
        self.authenticateErrorViewController = evc
    }


    func start() {
        authenticateViewController.name = "AUTH VC"
        authenticateViewController.delegate = self
        navigationController.setViewControllers([authenticateViewController], animated: true)
    }


    func end(){
        authenticateViewController.delegate = self
        navigationController.setViewControllers([authenticateErrorViewController], animated: true)
    }
}

// MARK: Authentication Delegate

extension LoginCoordinator: AuthenticateDelegate, AuthenticateErrorDelegate  {

    func jump() {
        delegate?.openAuthForm()
    }


}

// MARK: UIViewController Extension

extension UIViewController {

    func add(_ child: UIViewController) {
        addChild(child)
        view.addSubview(child.view)
        child.didMove(toParent: self)
    }

    func remove() {
        guard parent != nil else { return }
        willMove(toParent: nil)
        removeFromParent()
        view.removeFromSuperview()
    }
}

调试

AuthenticateDelegate 的调用工作正常:

import UIKit

protocol AuthenticateDelegate: class {
    func authenticateUser(username: String)
}

class AuthenticateViewController: UIViewController {
@IBOutlet weak var nameLabel: UILabel!
@IBOutlet weak var usernameField: UITextField!

var name: String?

weak var delegate: AuthenticateDelegate?

override func viewDidLoad() {
    super.viewDidLoad()

    nameLabel.text = name ?? nameLabel.text
}

@IBAction func loginAction(_ sender: Any) {
    guard let username = usernameField.text else { return }
    delegate?.authenticateUser(username: username)
}

}

我做错了什么?

提前致谢!

4

0 回答 0