如何在 macOS 下的非基于文档的应用程序中添加选项卡? [英] How to add Tabs in a non-document-based app under macOS?
问题描述
我喜欢使用 Xcode 8 在 Swift 3 中构建一个应用程序,它应该启用 Apple 选项卡栏.它不是基于文档的.我了解到here,如果我可以启用这些标签覆盖方法 @IBAction func newWindowForTab(_ sender: Any?)
例如在窗口控制器中.为了测试这一点,我使用故事板在 Xcode 中创建了一个新项目,添加了 NSWindowController
的子类并将其分配到故事板中.然后我实现了
I like to build an app in Swift 3 with Xcode 8 and it should enable the Apple tab bar. It is not document-based. I learned here, that the tabs can be enabled if I override the method @IBAction func newWindowForTab(_ sender: Any?)
for example in the window controller. To test this, I created a new project in Xcode using storyboard, added a subclass of NSWindowController
and assigned it in the storyboard. I then implemented
@IBAction override func newWindowForTab(_ sender: Any?) {}
在构建应用程序时会出现标签栏.重建后,我注意到+"按钮仅在应用程序在构建之前关闭时标签栏不可见时才会出现.那是一个错误吗?如何添加新标签?
and the tab bar appears when the app is build. Once rebuild, I noticed that the "+"-button only appears if the tab bar was not visible when the app is closed prior to build. Is that a bug? How would I add new tabs?
推荐答案
好的,这里有新文件,
应用委托
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
@IBAction func newWindowForTab(_ sender: Any?){
} // without this the + button doesn't show from start
}
视图控制器
import Cocoa
class ViewController: NSViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
}
和窗口控制器
import Cocoa
class WindowController: NSWindowController {
var subview: WindowController?
override func windowDidLoad() {
super.windowDidLoad()
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
@IBAction override func newWindowForTab(_ sender: Any?) {
let story = self.storyboard
let windowVC: WindowController = story?.instantiateInitialController() as! WindowController
self.window?.addTabbedWindow(windowVC.window!, ordered: .above)
self.subview = windowVC
windowVC.window?.orderFront(self.window)
windowVC.window?.makeKey()
}
}
您必须添加菜单项并将其连接到菜单视图中的 FirstResponder 到 newWindowForTab:操作,分配键,说 cmd+t 才能工作,这个例子只是从 + 按钮和窗口菜单选项添加选项卡,移动选项卡到新窗口"和合并所有窗口".您可以将标签拖出并放回,水平移动标签.看起来有效.
you have to add menu item and connect it to FirstResponder in menu view to newWindowForTab: action, assign key, say cmd+t to work, this example as is just adds tab from + button and window menu options work, "move tab to new window" and "merge all windows". You can drag tab out and drop back , move tabs horizontally. Look like it works.
使用 Xcode 版本 8.2 beta (8C30a) 完成
done with Xcode Version 8.2 beta (8C30a)
这篇关于如何在 macOS 下的非基于文档的应用程序中添加选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!