我如何以编程方式弹出ViewController(如模式)?

问题描述 投票:0回答:1

全部:

我正在一个快速,可可macos应用程序项目中,在一个情节提要中具有多个ViewController。

我知道我是否进行segue并将segue从第二个ViewController链接到第一个ViewController。

我可以弹出ViewController。

但是如果我有一个函数并想调用另一个ViewController从第一个ViewController中以编程方式呈现怎么办?

我搜索了很多示例,这些示例都是从UIStoryBoard开始的,但是我的Storyboard是NSStoryboard。有人可以提示我一点吗?

我的代码:

func checkPassword(SystemMsg:String) -> String{
        //print("x")
        let storyBoard = NSStoryboard(name: "Main", bundle: nil)
        let mainViewController : NSViewController = storyBoard.instantiateController(withIdentifier: "PasswordInput") as! NSViewController

        //self.present(mainViewController, asPopoverRelativeTo: <#T##NSRect#>, of: sender, preferredEdge: NSRectEdge, behavior: <#T##NSPopover.Behavior#>)

        return ""
    }

而且我在情节提要中的viewController看起来像(没有segue,没有链接):

enter image description here

如果有人可以逐步指导我,将不胜感激。

swift macos nsviewcontroller
1个回答
0
投票

我自己弄清楚了。

最简单的方法包含四个步骤:

  1. 在情节提要中确定您的主要ViewController和第二个ViewController
  2. 使用instantiateController创建主ViewController
  3. 使用instantiateController创建第二个ViewController
  4. 使用presentAsModalWindow或presentAsSheet在主ViewController上呈现secondController

首先,我们需要正确识别情节提要:

Identify first storyboard , we need to click top blue cube and then name it in Storyboard Id area

Identify second storyboard , we need to click top blue cube and then name it in Storyboard Id area

示例代码:

func checkPassword(SystemMsg:String) -> String{
        //print("x")
        //let storyBoard = NSStoryboard(name: "Main", bundle: nil)
        let mainViewController : NSViewController = self.storyboard?.instantiateController(withIdentifier: "MainController") as! NSViewController
        let passwordInputViewController : NSViewController = self.storyboard?.instantiateController(withIdentifier: "PasswordInput") as! NSViewController
        mainViewController.presentAsModalWindow(passwordInputViewController)

        //Or

        mainViewController.presentAsSheet(passwordInputViewController)

        return ""
    }

如果我想念什么,请轻轻地纠正我。

参考:Transitioning between view controller, OS X

PS。如果要在ViewController之间传递值,可以参考:https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/#back-properties


-1
投票

如果要从UIViewController的类中弹出UIViewController,则必须执行:

self.navigationController?.popViewController(animated: true)

如果您的UIViewController未嵌入到UINavigationController中,则必须这样做:

self.removeFromParentViewController()
© www.soinside.com 2019 - 2024. All rights reserved.