Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 253
Raymond Moay Avatar asked Jun 29 '16 07:06

Raymond Moay


2 Answers

Starting with iOS 8 you can use:

splitViewController?.showDetailViewController(vc, sender: self)

or if you want to replace primary controller

splitViewController?.show(vc, sender: self)
like image 156
Yaroslav Avatar answered Sep 19 '22 18:09

Yaroslav


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
like image 40
Altimir Antonov Avatar answered Sep 19 '22 18:09

Altimir Antonov