0

I am totally new to the Mac apps development. I am facing an issue since last two days but could not succeed. My problem is the same as question Swiching between 2 diferent NSViewControllers with data.

Could you please help me to understand the process and syntax of how to move from one NSViewController to another.

I have a View controller for login where I have two fields i.e. UserId and password. On the click of the login button a web API is called to authenticate the user and upon receiving "SUCCESS" as the response, control should be transferred from LoginViewController to ProfileViewController.

I have tried to resole this issue as per the answer of the question (link given) but I am getting an error that. "fromviewcontroller.view.superview cannot be nil."

Ashutosh Shukla
  • 358
  • 5
  • 14
  • Possible duplicate of [Swiching between 2 diferent NSViewControllers with data](https://stackoverflow.com/questions/48668457/swiching-between-2-diferent-nsviewcontrollers-with-data) – Ivan Ičin Jan 22 '19 at 12:40
  • Yes, I have mentioned the link in my question itself but it is not working for me. – Ashutosh Shukla Jan 22 '19 at 13:15
  • 1
    The question has the proper answer that fully explains everything. You haven't described your problem aside that it doesn't work and as it does you are probably doing something wrong. But unless you provide some more information this question needs to be closed IMHO. – Ivan Ičin Jan 22 '19 at 13:25

3 Answers3

0

Create a segue from LoginViewController to ProfileViewController and give an identifier to it like "showProfile".

@IBAction func loginButton(_ sender: Any) {

    // Code for validating User.

    if response == "SUCCESS" {
        performSegue(withIdentifier: "showProfile", sender: sender)
    }
}

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    if segue.identifier == "showProfile" {
        let profileController = segue.destinationController as! ProfileViewController
        profileController.data = responseData
    }
}

Not using storyboards?

@IBAction func loginButton(_ sender: Any) {

    // Code for validating User.

    if response == "SUCCESS" {
        let profileController = ProfileViewController(nibName: "ProfileViewController", bundle: Bundle.main)
        profileController.data = responseData
        self.view.window!.contentViewController = profileController
    }

}
Bharat Gadde
  • 173
  • 1
  • 10
0

Use these two function to go to the target NSViewcontroller/UIViewcontroller: (you must assign a unique storyboard id to the target view controller)

enter image description here

Mac APP:

 public func gotToNextVc(_ viewControllerIdentifier: String) {
        let nextVC = self.storyboard?.instantiateController(withIdentifier: NSStoryboard.SceneIdentifier(viewControllerIdentifier))
                    as! NSViewController
                self.view.window?.contentViewController = nextVC
        }

IOS APP:

public func gotToNextVc(_ viewControllerIdentifier: String){
    let storyBoard = UIStoryboard(name: ViewControllers.STORYBOARD, bundle: nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: viewControllerIdentifier)
    view.endEditing(true)
    nextViewController.modalPresentationStyle = .fullScreen
    nextViewController.modalTransitionStyle = .coverVertical
    self.present(nextViewController, animated: false, completion: nil)
    }
-1

You have to create a Segue in your Storyboard (Ctrl + Left click the yellow circle button above your LoginViewController and drag it to your ProfileViewController) and then name it something like "showProfile".

When you received your "SUCCESS" you want to call:

    //here you enter the name of the segue you want to call 
    //& in sender the data you want to pass to the new VC.
    performSegue(withIdentifier: "showProfile", sender: nil) 

this will call

    prepare(for segue: UIStoryboardSegue, sender: Any?) 

in your current ViewController, so if you want to pass data to your new ViewController, you need to override it.

Example for passing data between ViewControllers :

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        //asking which segue is called
        if segue.identifier == "showProfile" {  

            //when you called "showProfile", you can be sure that your
            //destination is a ProfileViewController
            let vc = segue.destination as! ProfileViewController 

            //passing the data to your new VC
            vc.data = sender
        }
    }
Bryan
  • 23
  • 7
  • Yellow circle and `UIStoryboardSegue` is iOS. The question is about `NSViewController` and macOS. – Willeke Jan 22 '19 at 11:56