3

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)
}

}

4

1 回答 1

1

任何对解决我遇到的视图问题的解决方案感兴趣的人,我只是用以下代码替换了视图,从 superview 中删除:self.ourViewController?.view.removeFromSuperview()

我遇到的问题是由于视图控制器是可选值。我的疏忽。

然后可以通过使用源列表的选择更改属性来调整侧边栏源列表以触发视图更改。

如果有人感兴趣,我将在完成后发布完整的解决方案。

于 2015-10-27T11:31:03.940 回答