Search code examples
macossidebarnssplitviewpxsourcelistnssplitviewcontroller

Source List Sidebar implementation in Swift with split view


I'm having issues trying to implement a navigation sidebar for an app. Considering that source lists are so prominent in OS X apps and that Apple's Human Interface Guidelines refer to a source list as an ideal way to navigate within an app, I'm surprised that there aren't more resources available (well not that I can find anyway). Everything that relates to split views that I can find seem to refer to ios.

I have no issues with implementing the content of the source list, my issue arises when it comes to view swapping based on the selection within the source list.

Essentially I want the user to select an option in the source list on the master side of a split view, and then for the corresponding view to be loaded in the detail side of the split view. I'm not sure if there is an easy way to implement something like this using storyboards as there is for a tabview controller for example? If not, is anyone able to suggest a basic implementation in swift to help get me started? I'm not sure where to start from the view swapping side of things.

Thanks in advance.

EDIT:

Further to my previous post, I have included below a basic implementation of a view swap but when the view is replaced, the previous view remains. In addition to my questions about, how do I remove the previous view before adding a new one? My line of code removeFromSuperView() seems to be causing major problems!

import Cocoa

class AppController: NSObject {

@IBOutlet weak var ourView: NSView!
var ourViewController: NSViewController!


let kFirstViewTag = 0
let kSecondViewTag = 1
let kFirstView = "FirstViewController"
let kSecondView = "SecondViewController"



@IBAction func changeView(sender: NSPopUpButton) {
    let tag = sender.selectedTag()
    self.changeViewController(tag)


}

func changeViewController(tag: Int){

    ourViewController.view.removeFromSuperview()


    switch tag{
    case kFirstViewTag:
        self.ourViewController = FirstViewController(nibName: kFirstView, bundle: nil)

    case kSecondViewTag:
        self.ourViewController = SecondViewController(nibName: kSecondView, bundle: nil)

    default: print("There was an error with the view controller change")

    }

    ourView.addSubview(ourViewController.view)

}

override func awakeFromNib() {
    self.changeViewController(kFirstViewTag)
}

}


Solution

  • Anybody interested in a solution to the removal of the view problem I was experiencing, I simply replaced the view, remove from superview with the following code: self.ourViewController?.view.removeFromSuperview()

    The problem I was experiencing was due to the view controller being optional value. Slight oversight on my part.

    The sidebar source list is then able to be adapted by using the selection change property of the source list to trigger the view change.

    I will post a full solution once completed if anyone is interested.