使用KYDrawerController遇导航崩溃、初始视图失效 求排查帮助
Hey there! Let's dig into the two issues you're hitting with KYDrawerController and get them sorted out. It makes total sense to suspect the AppDelegate since that's where your app's root controller setup lives—let's break this down step by step.
Issue 1: App Crashes When Tapping Buttons (Even With No Logic)
This usually boils down to problems with your view controller hierarchy, memory management, or missing navigation context. Here are the most likely fixes:
- Check if KYDrawerController is properly retained: You've declared
drawerControlleras a property in AppDelegate, which is good (avoids it being deallocated prematurely). Double-check that you're not accidentally reinitializing it as a local variable somewhere else. - Verify your rootViewController setup: Your app's window must have the drawerController set as its root—don't skip this step. If your main content or sidebar VC is directly set as the root, the drawerController's hierarchy breaks, leading to crashes when trying to navigate.
- Use the correct navigation context: If you're trying to push/present a new VC from a button, make sure you're using the right navigation controller. For example, if your main content VC is wrapped in a
UINavigationController, use:
Using@IBAction func buttonTapped(_ sender: UIButton) { let newVC = TargetViewController() self.navigationController?.pushViewController(newVC, animated: true) }self.present(...)should also work, but crashes here often mean the current VC's parent hierarchy is broken. - Check crash logs: The console error (like
EXC_BAD_ACCESSfor memory issues orunrecognized selector sent to instance) will tell you exactly what's wrong—don't ignore this!
Issue 2: Custom Initial View Doesn't Take Effect
This almost always relates to how you're configuring the drawerController's main/sidebar VCs or setting up the window. Try these fixes:
- Follow the full KYDrawerController setup flow: Make sure you're assigning your initial view controller to the drawerController before setting it as the window's root. Here's a complete example for AppDelegate (iOS 12 or earlier):
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // 1. Create your desired initial main view controller let initialMainVC = YourCustomInitialViewController() let mainNav = UINavigationController(rootViewController: initialMainVC) // 2. Create your sidebar controller let sideMenuVC = YourSideMenuViewController() // 3. Configure the drawer controller drawerController.mainViewController = mainNav drawerController.leftViewController = sideMenuVC // Use rightViewController for right-side menu // 4. Set as window root window = UIWindow(frame: UIScreen.main.bounds) window?.rootViewController = drawerController window?.makeKeyAndVisible() return true } - iOS 13+? Check SceneDelegate: If you're targeting iOS 13 or later, the root controller setup should happen in
SceneDelegate'sscene(_:willConnectTo:options:)method instead of AppDelegate. If you're setting it in both places, the SceneDelegate code might be overriding your AppDelegate setup. - Avoid overwriting the rootViewController: Make sure no other part of your app (like a splash screen VC) is resetting the window's rootViewController after you've set up the drawerController.
Next Steps to Debug Faster
To narrow this down even more, sharing these details would help:
- The exact crash message from your Xcode console
- The full code of your AppDelegate (or SceneDelegate, if applicable)
- The code for your button's IBAction (even if you think it's empty)
内容的提问来源于stack exchange,提问作者William Loke
相关产品推荐
相关产品推荐

