0

All:

I am doing a swift , cocoa macos app project with multiple ViewControllers in one storyboard.

I know if I do segue and link the segue from second ViewController to first ViewController.

I can pop out ViewController.

But what if I have a function and want to call another ViewController to present programmatically from first ViewController?

I search a lot of examples start with UIStoryBoard, but my Storyboard is NSStoryboard. Could anyone hint me a little to start with?

my code:

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 ""
    }

And my viewController in storyboard look like(no segue,no link):

enter image description here

If anyone can guide me through this step by step would be appreciated.

Ming
  • 1
  • 3
  • Does this answer your question? [Cocoa - Present NSViewController programmatically](https://stackoverflow.com/questions/36263990/cocoa-present-nsviewcontroller-programmatically) – Willeke Mar 10 '20 at 10:45
  • No, the question is similar to mine. But the feedback didn't answer it with detail. – Ming Mar 11 '20 at 01:57
  • Currently I am examing this article for more details :https://stackoverflow.com/questions/28454291/transitioning-between-view-controller-os-x – Ming Mar 11 '20 at 02:05

1 Answers1

0

I figure it out myself.

The most simple way contains 4 steps:

  1. Identify your main ViewController and second ViewController in storyboard
  2. Create your main ViewController with instantiateController
  3. Create your second ViewController with instantiateController
  4. use presentAsModalWindow or presentAsSheet to present secondController on main ViewController

first we need to Identify storyboard correctly:

Identify first storyboard , we need to click top blue cube and then name it in Storyboard Id area
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
Identify second storyboard , we need to click top blue cube and then name it in Storyboard Id area

example code:

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 ""
    }

If I miss something please correct me gently.

reference: Transitioning between view controller, OS X

PS. if you want to pass value between ViewController, this is good reference : https://learnappmaking.com/pass-data-between-view-controllers-swift-how-to/#back-properties

de.
  • 7,068
  • 3
  • 40
  • 69
Ming
  • 1
  • 3