【问题标题】:coordinator is nil and not navigating to the next screen on button clickcoordinator 是 nil 并且没有在按钮点击时导航到下一个屏幕
【发布时间】:2021-07-17 05:05:16
【问题描述】:

我一直在尝试重构我的源代码,使其符合协调器模式。我使用UITabBarController 作为我的应用程序的父视图控制器,其中包含 4 个视图控制器。

我一直在关注如何为 iOS 应用程序实现协调器模式的教程,并且我已经创建并设置了协调器的协议和类。我的视图控制器(TabbarViewController 的子视图控制器)中有一个按钮,但是,在单击按钮时,协调器没有推送/导航到所需的 VC,并且我看到协调器在调试控制台上返回 nil,同时通过breakpoint,我不知道如何解决这个问题。

MainCoordinator.swift:

class MainCoordinator: SubCoordinator {
    var subCoordinators = [SubCoordinator]()
    var navigationController: UINavigationController
    
    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }
    func start() {
        print("Initialized.. .")
        UIApplication.app().window?.rootViewController = self.navigationController
        let vc = SplashViewController.instantiate()
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: false)
    }
        }
   // testing using a simple Viewcontroller class, its background color is set to red, so if the  
   // navigation works, a blank red VC should appear. but not working so far
    func testView() {
        let vc = ViewController.instantiate()
        vc.coordinator = self
        navigationController.pushViewController(vc, animated: false)
       
    }
}

SubCoordinator.swift:

protocol SubCoordinator {
    var subCoordinators: [SubCoordinator] { get set }
    var navigationController: UINavigationController { get set }
    
    func start()
}

StoryBoarded.swift:

protocol StoryBoarded {
    static func instantiate() -> Self
}
  // I am using storyBoard, and `instantiate()` should instantiate and return the specified VC 
  // from the Storyboard with the specified VC id (?)
extension StoryBoarded where Self: UIViewController {
    static func instantiate() -> Self {
        let id = String(describing: self)
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        return storyboard.instantiateViewController(withIdentifier: id) as! Self
    }
}

FirstViewController.Swift:

class FirstViewController: UIViewController, StoryBoarded {
    @IBOutlet weak var button: UIButton!
    var coordinator: MainCoordinator?
    //MARK: - viewDidLoad()
    override func viewDidLoad() {
        super.viewDidLoad()
   // If uncommented the below line, coordinator is not returning `nil`, but not navigating 
      anyways!
        //coordinator = MainCoordinator(navigationController: UINavigationController())
    }
    @IBAction func onButtonTap(_ sender: Any) {
   // So, basically I would expect the coordinator to navigate to the testView, but not 
      navigating
        coordinator?.testView()
    }
}

ViewController.swift:

// testView
class ViewController: UIViewController, StoryBoarded {
    var coordinator: MainCoordinator?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.view.backgroundColor = .red
    }

}

// TabbarController, set as the root VC after the splashVC is completed
class MainViewController: UITabBarController, StoryBoarded {
    var coordinator: MainCoordinator?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let firstVC = UIStoryboard.firstViewController()
        let secondVC = UIStoryboard.secondViewController()
        
        let views: [UIViewController] = [firstVC, secondVC]
        self.setViewControllers(views, animated: false)
        self.navigationController?.navigationBar.isHidden = false
    }
    
}

start() 正在被调用,并且 splashVC 出现并在完成时使用 MainViewontroller 更新 rootViewController,但是在按钮单击事件上导航根本不起作用。

任何反馈或帮助将不胜感激!

【问题讨论】:

    标签: ios swift mvvm coordinator-pattern


    【解决方案1】:

    由于您使用的是 StoryBoarded 协议,因此您应该遵循该模式并调用 instantiate() 进行初始化。然后,只需设置协调器。

    class MainViewController: UITabBarController, StoryBoarded {
      var coordinator: MainCoordinator?
    
      override func viewDidLoad() {
        super.viewDidLoad()
        let firstVC = FirstViewController.instantiate()
        let secondVC = SecondViewController.instantiate()
    
        firstVC.coordinator = self.coordinator
        secondVC.coordinator = self.coordinator
        
        let views: [UIViewController] = [firstVC, secondVC]
        self.setViewControllers(views, animated: false)
        self.navigationController?.navigationBar.isHidden = false
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多