Search code examples
iosswiftuisplitviewcontrollerappdelegatemaster-detail

How to load a detailed view controller (from split view) on start up in swift?


When my app starts for the first time on an iPad simulator, the mainVC table loads and lists data from my model, but the detail view controller doesn't. It only loads and lists data from the storyboard. I'd like it to list the first item from the main menu (for example).

I saw this done in a RayW tutorial from the AppDelegate method, didFinishLaunchingWithOptions, but they didn't use the default split view code, but rather re-created it from scratch with some variation from the default code. This is how they did it:

let splitViewController = self.window!.rootViewController as! UISplitViewController
let leftNavController = splitViewController.viewControllers.first as! UINavigationController
let masterViewController = leftNavController.topViewController as! MasterViewController
let detailViewController = splitViewController.viewControllers.last as! DetailViewController 
let firstMonster = masterViewController.monsters.first
detailViewController.monster = firstMonster
return true

The following is the default code plus (after the empty line) an attempt to reconcile the default code with the above method.

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    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 leftNavController = splitViewController.viewControllers.first as! UINavigationController
    let masterViewController = leftNavController.topViewController as! MasterViewController
    let detailViewController = navigationController.topViewController as! DetailViewController
    let firstRecord = masterViewController.records.first
    detailViewController.record = firstRecord

    return true
}

In my initial attempts, I was casting around the wrong controllers, but the above code at least compiles and runs, but it does not load the first record into the detailVC.

Is this the right approach and I am just missing something, or is there another way to do this?


Solution

  • The easier way to do this is when the detailedVC is instanciated in the MasterViewController's viewDidLoad method:

    override func viewDidLoad() 
    {
        super.viewDidLoad()
    
        startingDetailedVCData = theData
    
        if let split = self.splitViewController {
            let controllers = split.viewControllers
            self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
    
            self.detailViewController?.detailedData = startingDetailedVCData
        }
    }