Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift macOS应用程序,如macOS上的Photos.app_Swift_Macos_Nswindow_Nsviewcontroller_Nswindowcontroller - Fatal编程技术网

Swift macOS应用程序,如macOS上的Photos.app

Swift macOS应用程序,如macOS上的Photos.app,swift,macos,nswindow,nsviewcontroller,nswindowcontroller,Swift,Macos,Nswindow,Nsviewcontroller,Nswindowcontroller,我正在尝试创建一个类似Photos.app的macOS应用程序。NSWindowController有一个带有分段控件的工具栏。点击分段控件时,它会更改NSWindowController中的NSViewController 到目前为止,我所拥有的是一个带有NSViewController的NSWindowController。我有一个子类NSWindowController,其中我有一个方法,每当用户点击分段控件时就会调用该方法 本质上,无论单击哪个段,它都会实例化所需的视图控制器,并将其设

我正在尝试创建一个类似Photos.app的macOS应用程序。
NSWindowController
有一个带有分段控件的工具栏。点击分段控件时,它会更改
NSWindowController
中的
NSViewController

到目前为止,我所拥有的是一个带有
NSViewController
NSWindowController。我有一个子类
NSWindowController
,其中我有一个方法,每当用户点击分段控件时就会调用该方法

本质上,无论单击哪个段,它都会实例化所需的视图控制器,并将其设置为
NSWindowController
contentViewController
属性

这是正确的做法吗

另外,我认为
NSWindowController
应该为每个
NSViewController
都有属性,它可以切换到延迟加载(当用户点击它们时加载,它们会被保留以重新使用以防止重新初始化)

代码:

import Cocoa

class MainWindowController: NSWindowController
{
    var secondaryViewController:NSViewController?

    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 func segmentedControlDidChange(_ sender: NSSegmentedControl)
    {
        print("Index: \(sender.selectedSegment)")

        if sender.selectedSegment == 3 {
            if secondaryViewController == nil {
                let viewController = storyboard?.instantiateController(withIdentifier: "SecondaryViewController") as! NSViewController
                secondaryViewController = viewController
            }

            self.window?.contentViewController = self.secondaryViewController
        }
    }
}

我是macOS开发的新手,但是,我做iOS已经有一段时间了。如果有更好的办法,我想知道。谢谢

要将选项卡/分段控件移动到标题栏,需要:

  • 将工具栏添加到窗口,并将控件添加到工具栏
  • 隐藏标题:

    class TopLevelWindowController: NSWindowController {
    
      override func windowDidLoad() {
        super.windowDidLoad()
    
        if let window = window {
    
            // reminder like style
            // window.titlebarAppearsTransparent = true
            window.titleVisibility = .hidden
            // window.styleMask.insert(.fullSizeContentView)
        }
      }
    }
    
  • 现在,工具栏将合并到顶部栏的位置


  • 您也可以考虑使用NStaveVIEW控制器。@ JTBANDES这是正确的,但我试图保持苹果公司的其他MACOS应用程序的一致性外观,如照片、注释和地图。我需要分段控件与搜索栏和按钮等其他东西一起保留在工具栏中。谢谢你的意见!NSTabViewController可以在没有可见选项卡的情况下使用,仅用于处理内容VCs之间的切换。