0

I am developing a simple IOS application using swift. In my application I need to open new controller programatically from another controller. So I added another scene to the storyboard.

This is the screenshot enter image description here

Then I added a new class for new controller which is inheriting from the UIViewController. This is the code for new controller

import UIKit

class ReplayController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

Then, I tried to open the new controller view (ReplayController) from the main controller like this in code.

func gameOver()
    {
        let replayController = ReplayController()
        present(replayController, animated: true, completion: nil)
    }

When I call that function, it just pops up the blank screen. Nothing appear on the screen. What is wrong and how can I solve it?

3

3 Answers 3

2

In your Storyboard file, ensure the ReplayController has its 'File's Owner' set to your ReplayController class. Then set the Storyboard ID like below:

enter image description here

Then you can load it like so:

let replay = storyboard?.instantiateViewController(withIdentifier: "ReplayController") as! ReplayController
self.present(replay, animated: true, completion: nil) 
Sign up to request clarification or add additional context in comments.

1 Comment

There is no storybord id filed. I could only find RestorationID
2

You have to reference it with id in storyboard

 let replayController = self.storyboard?.instantiateViewController(withIdentifier: "replayControllerID") as! ReplayController

 present(replayController, animated: true, completion: nil) 

as this line only

let replayController = ReplayController()

doesn't load the xib or storyboard object associated with the VC

Comments

1

When you design your view controllers in storyboard, you need to programatically create them using the method instantiateViewController of class UIStoryboard to access it along with all the outlets.

Before that, you need to set the Storyboard ID of the respective view controller in your storyboard like this:

Storyboard ID Screenshot

And then present it:

func gameOver() {
    if let myVC = storyboard?.instantiateViewController(withIdentifier: "ViewController") {
        self.present(myVC, animated: true, completion: nil)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.