简体   繁体   English

在Mac的Swift Storyboard中从一个NSViewController切换到另一个

[英]Switching from one NSViewController to another in Swift Storyboards for mac

I have a problem which I am trying to solve for quite a while now. 我有一个想解决很长一段时间的问题。 Project file provided also. 还提供了项目文件。

So I have the following idea, have a sidebar with buttons, and when the buttons are clicked it switches only the right side content. 所以我有以下想法,有一个带有按钮的侧栏,单击按钮时,它仅切换右侧内容。

I originally created a project that has just 1 storyboard. 我最初创建的项目只有一个情节提要。 Window launches the first ViewController, with buttons and a custom view. Window将启动第一个ViewController,其中包含按钮和自定义视图。

What i need is when one of the buttons is pressed that custom view is replaced with contents of another ViewController. 我需要的是,当按下其中一个按钮时,自定义视图将替换为另一个ViewController的内容。 Just the view, not the entire previous view controller replaced by another one. 只是视图,而不是将整个先前的视图控制器替换为另一个视图控制器。

How do i do that? 我怎么做? Segues? 塞格斯? Layers? 层? Please desperately need help as I cant figure out storyboards but want to work with them a lot. 迫切需要帮助,因为我无法弄清楚情节提要,但希望与他们合作很多。 This will get me started. 这会让我开始。

One other option I was thinking is if I used splitview would it be any better? 我在想的另一个选择是,如果我使用splitview会更好吗? Then i could probably replace right side view sontroller with another one entirely right? 然后,我可能可以用另一个完全正确的右视图Sontroller代替?

Link to project file in Xcode 链接到Xcode中的项目文件

我的故事板

I create this with textlable but is the same. 我使用textlable创建此文件,但相同。

  1. Create "@IBOutlet weak var labelListInvoices : NSTextField!" 创建“ @IBOutlet弱var labelListInvoices:NSTextField!”
  2. Create action "@IBAction func onListInvoices(_ sender: AnyObject) { let act = InvoicesList(nibName: "InvoicesList", bundle: nil) app.show(act!)" 创建操作“ @IBAction函数onListInvoices(_发件人:AnyObject){let act = InvoicesList(nibName:“ InvoicesList”,包:nil)app.show(act!)”
  3. Create a var "var app = NSApp.delegate as! AppDelegate" 创建一个变量“ var app = NSApp.delegate as!AppDelegate”
  4. Now go to appdelegate and create this func "// APP DIRECTOR 现在转到appdelegate并创建此func“ // APP DIRECTOR

    // Use: // let act = MainView(nibName: "MainView", bundle: nil) // app.show(act!) func show(_ newController: ControllerType, with message: Parameters? = nil) { let newView = newController.view //使用:// let act = MainView(nibName:“ MainView”,bundle:nil)// app.show(act!)func show(_ newController:ControllerType,带有消息:Parameters?= nil){let newView = newController.view

     if let frame = NSApp.mainWindow?.contentView?.frame { newView.frame = frame // Resize the view to the window instead } guard let main = NSApp.mainWindow?.contentViewController else { return } if main.childViewControllers.count < 1 { // if no controllers, then it's main controller main.addChildViewController(newController) // add new controller main.view.addSubview(newView) // add new view to main view if message != nil { newController.notify(message) } } else { let current = main.childViewControllers.last // get lasr controller as current before adding new main.addChildViewController(newController) // add new controller main.transition(from: current!, to: newController, options: [.crossfade]){ if message != nil { newController.notify(message) } } } 

    } " }“

You can try to use NSTabView and insert NSTabViewItem objects as much as you need. 您可以尝试使用NSTabView并根据需要插入NSTabViewItem对象。 Each NSTabView can be initiated with custom NSViewController. 每个NSTabView都可以使用自定义NSViewController启动。 To hide default tab buttons you can set border type of NSTabView to None. 若要隐藏默认选项卡按钮,可以将NSTabView的边框类型设置为“无”。 Tabs can be switched programmatically via selectTabViewItem(). 可以通过selectTabViewItem()以编程方式切换选项卡。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM