I'm new to Swift.
I want to build a Master-detail view which is not initial of the app. The AppDelegate.swift generated by Xcode using Master-detail Application just go crash when I set other view before the Master-detail view.
Here's the code of AppDelegate.swift:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let splitViewController = self.window!.rootViewController as! UISplitViewController
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
splitViewController.delegate = self
let masterNavigationController = splitViewController.viewControllers[0] as! UINavigationController
let controller = masterNavigationController.topViewController as! MasterViewController
controller.managedObjectContext = self.managedObjectContext
return true
}
And I got the following error:
Could not cast value of type 'UIViewController' (0x11079e418) to 'UISplitViewController' (0x1107a8eb8).
(lldb)
And my Main.storyboard:
Thanks for help!
Your initial view controller is a
UIViewController
but in the beginning ofapplication(application:, didFinishLaunchingWithOptions:)
you are still acting as though the first view onscreen is aUISplitViewController
(seelet splitViewController = self.window!.rootViewController as! UISplitViewController
).In order to have a different view be the initial view controller you need to update that code appropriately to handle the different view controller being first.