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?

Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • Possible duplicate of [Instantiate and Present a viewController in Swift](https://stackoverflow.com/questions/24035984/instantiate-and-present-a-viewcontroller-in-swift) – trungduc Apr 19 '18 at 15:05
  • `ReplayController()` That's creating a New object. But not a `ReplayController` object initialized with the UI part in the Storyboard. – Larme Apr 19 '18 at 15:05
  • Possible duplicate of [How to switch view controllers in swift?](https://stackoverflow.com/questions/25375409/how-to-switch-view-controllers-in-swift) – J. Doe Apr 19 '18 at 15:06

3 Answers3

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

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
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) 
badhanganesh
  • 3,427
  • 3
  • 18
  • 39
Daven
  • 549
  • 4
  • 11
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)
    }
}
badhanganesh
  • 3,427
  • 3
  • 18
  • 39