I am trying to push another VC from UICollectionView
DidSelect
delegate, it is being called, but oddly, it is not pushing the initiated
VC, how can i fix it?
my AppDelegate.swift
looks like this:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
weak var coordinator: MainCoordinator?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.main.bounds)
coordinator = MainCoordinator(navigationController: UINavigationController())
self.window?.rootViewController = UIStoryboard.splashViewController()
self.window?.makeKeyAndVisible()
return true
}
}
and my SplashViewController
, where I switch the RootVC
to MainVC
in DispatchQueue
after the SplashViewController
completed:
class SplashViewController: UIViewController {
weak var coordinator: MainCoordinator?
//MARK: -viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
// MARK: - viewDidAppear()
override func viewDidAppear(_ animated: Bool) {
let animationView = AnimationView()
let animation = Animation.named("SplashAnimation", bundle: Bundle.main)
animationView.animation = animation
animationView.frame = CGRect(origin: .zero, size: CGSize(width: self.view.frame.size.width, height: 200))
animationView.center = self.view.center
animationView.loopMode = .playOnce
animationView.contentMode = .scaleAspectFit
animationView.play { (finished) in
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
if let vc = self.storyboard?.instantiateViewController(identifier: "MainViewController") as? MainViewController {
self.view.window?.rootViewController = vc
self.view.window?.makeKeyAndVisible()
}
})
}
view.addSubview(animationView)
}
}
and in my UICollectionView
's DidSelect
delegate, I am doing this:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if collectionView == categoryCollectionView {
let vc = self.storyboard?.instantiateViewController(identifier: "SymptomViewController") as! SymptomViewController
self.navigationController?.pushViewController(vc, animated: true)
} else {
print("next collectionView not ready yet.. .")
}
}
which usually runs expected, but now it is printing the print
statement but never pushing the next VC.. .
First of all, you need to make sure that if collectionView == categoryCollectionView
really equals true.
Second, I noticed that your MainViewController
doesn't seem to be embedded in a NavigationController
, so when you call self.navigationController?.pushViewController(vc, animated: true)
, it's possibly going to do nothing as the optional chaining will detect that self.navigationController is nil
.
Due to my low reputation I cannot add this as a comment, therefore I am posting it in this way.
Have you tried placing a breakpoint on the first line of the collectionView delegate method and checking the collectionView with "po" command in debugger? Since your if statement fails, most probably it is some other collectionView triggering this delegate method. You have to make sure that if collectionView == categoryCollectionView
truly returns true.
Also, once app execution is halted on the breakpoint, it is often very useful to check the debug navigator (in case you haven't yet) and get to know what lead to the specified breakpoint.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.