这是AppDelegate.swift的实现/配置:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var coordinator: MainCoordinator?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Fabric.with([Crashlytics.self])
// FirebaseApp.configure()
let navigationController = UINavigationController()
coordinator = MainCoordinator(navigationController: navigationController)
coordinator?.start()
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
这里是MainCoordinator.swift
import Foundation
导入UIKit
Class MainCoordinator:协调人{ var subCoordinators =协调器 var navigationController:UINavigationController
init(navigationController: UINavigationController) {
self.navigationController = navigationController
}
func start() {
let VC = SplashViewController.instantiate()
VC.coordinator = self
navigationController.pushViewController(VC, animated: false)
NSLog("view Started")
}
func goToMerchandizeEntranceView() {
let vc = MerchandizeEntranceViewController.instantiate()
vc.coordinator = self
navigationController.pushViewController(vc, animated: true)
}
func gotoMerchandizeRequestView() {
let vc = MerchandizeRequestStatusViewController.instantiate()
vc.coordinator = self
navigationController.pushViewController(vc, animated: true)
}
}
这里MainVC中,在View集合索引的didselectItemAt IndexPath方法中应该触发到下一个VC,但是,不会触发VC并显示在secure上,但是“ NsLog”会打印该消息。任何人都可以帮助我解释我的错误之处。 。?
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == menuCollectionView {
switch indexPath.row {
case 0:
coordinator?.goToMerchandizeEntranceView()
NSLog("Receiving touch")
case 1:
coordinator?.gotoMerchandizeRequestView()
NSLog("Receiving touch")
default:
break
}
}
else {
NSLog("define action for story cell")
}
}
更新:由于您正在使用情节提要,因此您应该从情节提要中实例化视图控制器,如下所示:
func goToMerchandizeEntranceView() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: "MerchandizeEntranceViewController") as? MerchandizeEntranceViewController {
vc.coordinator = self
navigationController.pushViewController(vc, animated: true)
} else {
print("Could not instantiateViewController: MerchandizeEntranceViewController")
}
}
func gotoMerchandizeRequestView() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyboard.instantiateViewController(withIdentifier: "MerchandizeRequestStatusViewController") as? MerchandizeRequestStatusViewController {
vc.coordinator = self
navigationController.pushViewController(vc, animated: true)
} else {
print("Could not instantiateViewController: MerchandizeEntranceViewController")
}
}
似乎您的协调器未在MainVC
中初始化。您应该在viewDidLoad
中初始化协调器:
override func viewDidLoad() {
super.viewDidLoad()
if let navigationController = navigationController {
coordinator = MainCoordinator(navigationController: navigationController)
}
}