Swift 中的 MacOS 菜单
MacOS menu in Swift
我想在我的应用程序中创建一个菜单边栏,它可以显示和控制除菜单之外的视图控制器。像下面这样的东西:
我想避免在菜单中使用 TableView,但我并不完全反对。我试过使用 TableView 和 Container,以及 TabViewController,但我似乎都无法像上面那样。
感谢任何帮助,提前致谢。
灵感来自"What you are looking for is called NSSplitViewController"
代码如下:
class MenuController: NSViewController {
override func loadView() {
super.loadView()
tabViewController = parent?.childViewControllers[1] as! NSTabViewController! //The parent is the SplitView, so the child in the second view would be the TabViewController
}
@IBOutlet weak var FirstButton: NSButton!
@IBOutlet weak var SecondButton: NSButton!
var tabViewController = NSTabViewController()
@IBAction func FirstView(_ sender: Any) {
tabViewController?.selectedTabViewItemIndex = 0 //Now that the TabViewController is specified, one may set the current view controller within the tabview.
}
@IBAction func SecondView(_ sender: Any) {
tabViewController?.selectedTabViewItemIndex = 1 //Shows the second view in NSTabViewController
}
}
图中,NSSplitViewController
有两个childViewControllers
;因此它是那些控制器的父级,可以通过两个子视图控制器中的 parent?
方法访问。一旦您指定了 tabViewController
,您就可以设置它的 selectedTabViewItemIndex[value]
来切换 NSTabViewController
.
中的视图
我想在我的应用程序中创建一个菜单边栏,它可以显示和控制除菜单之外的视图控制器。像下面这样的东西:
感谢任何帮助,提前致谢。
灵感来自"What you are looking for is called NSSplitViewController"
代码如下:
class MenuController: NSViewController {
override func loadView() {
super.loadView()
tabViewController = parent?.childViewControllers[1] as! NSTabViewController! //The parent is the SplitView, so the child in the second view would be the TabViewController
}
@IBOutlet weak var FirstButton: NSButton!
@IBOutlet weak var SecondButton: NSButton!
var tabViewController = NSTabViewController()
@IBAction func FirstView(_ sender: Any) {
tabViewController?.selectedTabViewItemIndex = 0 //Now that the TabViewController is specified, one may set the current view controller within the tabview.
}
@IBAction func SecondView(_ sender: Any) {
tabViewController?.selectedTabViewItemIndex = 1 //Shows the second view in NSTabViewController
}
}
图中,NSSplitViewController
有两个childViewControllers
;因此它是那些控制器的父级,可以通过两个子视图控制器中的 parent?
方法访问。一旦您指定了 tabViewController
,您就可以设置它的 selectedTabViewItemIndex[value]
来切换 NSTabViewController
.