Search code examples
iosxcodeswiftuisplitviewcontroller

How to change detail View in splitViewController programatically in Swift


I'm making an app where I have a key/value in NSUserDefaults which allows the app on start up to detect if this is the first time the app is turned on on the device. If it's the first time, I want the splitVC(which is also the rootVC)'s detail View to be my pageViewController(tutorial), else I want it to go straight into the app (another view controller, lets call it todayViewController).

I currently have a class for my SplitVC (GlobalSplitViewController.swift), but I currently have no idea how to programmatically change the detail view in ViewDidLoad.

Also, in storyboard, my splitVC's detail segue is connected to todayViewController and it's master segue to a menuVC, which is working perfectly.

Thanks in advance!

Code in GlobalSplitViewController.swift:

import UIKit

class GlobalSplitViewController: UISplitViewController, UISplitViewControllerDelegate {

var firstTime: Bool!

override func viewDidLoad() {
    super.viewDidLoad()

    self.delegate = self
    firstTime = loadFistTime()

    if firstTime == true {
    //load tutorials pageVC
    } else {
   //load todayVC
    }

}

func splitViewController(svc: UISplitViewController, shouldHideViewController vc: UIViewController, inOrientation orientation: UIInterfaceOrientation) -> Bool {
    return true
}

Solution

  • In AppDelegate for example you can check your UserDefaults, and with Switch or If/else you can change the splitView. Here is an example of changing the detailViewController.

    let detailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("DetailNavigationViewController") as! UINavigationController
    self.splitViewController?.viewControllers[1] = detailViewController