gpt4 book ai didi

xcode - 如何在macOS下的非基于文档的应用程序中添加选项卡?

转载 作者:行者123 更新时间:2023-12-04 11:20:47 24 4
gpt4 key购买 nike

我喜欢用Xcode 8在Swift 3中构建一个应用程序,它应该启用Apple标签栏。它不是基于文档的。我了解到here,如果我例如在窗口 Controller 中重写@IBAction func newWindowForTab(_ sender: Any?)方法,则可以启用选项卡。为了测试这一点,我使用 Storyboard 在Xcode中创建了一个新项目,添加了NSWindowController的子类并将其分配到 Storyboard 中。然后我实现了

@IBAction override func newWindowForTab(_ sender: Any?) {}

并在构建应用程序时显示选项卡栏。重建后,我注意到只有在构建之前关闭应用程序时标签栏不可见时,才会出现“+”按钮。那是个错误吗?如何添加新标签?

最佳答案

好的,这是新文件,

代理

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

}

您必须添加菜单项并将其在菜单 View 中的FirstResponder上连接到newWindowForTab:操作,分配键,说cmd + t起作用,此示例仅是通过+按钮添加选项卡,并且窗口菜单选项起作用,“将选项卡移至新窗口”和“合并所有窗口”。您可以将标签页拖出并放回,水平移动标签页。
看起来很有效。

使用Xcode 8.2 beta(8C30a)完成

关于xcode - 如何在macOS下的非基于文档的应用程序中添加选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40202386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com