如何在macOS下的非基于文档的应用程序中添加选项卡? [英] How to add Tabs in a non-document-based app under macOS?
问题描述
我喜欢用Xcode 8在Swift 3中构建一个应用程序,它应该启用Apple标签栏.它不是基于文档的.我此处了解到,如果我可以启用这些标签覆盖例如窗口控制器中的方法@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?
推荐答案
好,这是新文件,
Appdelegate
Appdelegate
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
}
ViewController
ViewController
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.
}
}
}
和WindowController
and WindowController
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屋!