Search code examples
swiftuiviewcontrollerviewcontroller

programmatically present a new view controller in swift/programmatically give a class an Identifier


I've already created a simple app that uses the storyboard to present a new page/class. However, I would like to do the same thing, programmatically.

My initial ViewController.swift file looks like this:

import UIKit

class ViewController: UIViewController {


    var mainImageView: UIImageView!
    var chooseButton: UIButton!
    var nameLabel: UILabel!

    override func loadView() {
        view = UIView()
        view.backgroundColor = .white

        let chooseButton = UIButton(type: .custom) as UIButton
        chooseButton.backgroundColor = .blue
        chooseButton.layer.borderColor = UIColor.darkGray.cgColor
        chooseButton.layer.borderWidth = 2
        chooseButton.setTitle("Pick a side", for: .normal)
        chooseButton.frame = CGRect(x: 100, y: 100, width: 200, height: 100)
        chooseButton.addTarget(self, action: #selector(clickMe), for: .touchUpInside)
        chooseButton.layer.cornerRadius = chooseButton.frame.size.height/2
        self.view.addSubview(chooseButton)

        self.nameLabel = UILabel()

        nameLabel.text = "Here is your side"
        nameLabel.textAlignment = .center
        nameLabel.backgroundColor = .cyan
        nameLabel.frame = CGRect(x: 100, y: 400, width: 200, height: 100)
        self.view.addSubview(nameLabel)


    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    @objc func clickMe(sender:UIButton!) {
        print("Button Clicked")
        self.nameLabel.text = "updated title"
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

        let loadVC = storyBoard.instantiateViewController(withIdentifier: "SelectionScreen") as! SelectionScreen
        self.present(loadVC, animated: true, completion: nil)


    }

When I press the button, I receive the following error:

terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard () doesn't contain a view controller with identifier 'SelectionScreen''

I created the next Viewcontroller using Cocoa Touch Class:

import UIKit

class SelectionScreen: UIViewController {


    override func loadView() {
        view = UIView()
        view.backgroundColor = .red




    }
    override func viewDidLoad() {
        super.viewDidLoad()

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



}

When I originally did this with the Storyboard, I used the Identity Inspector to set the Storyboard ID so that instantiateViewController(withIdentifier:) could load the new class I wanted to view. How do I provide an identifier for the SelectionScreen class, without using the Storyboard?


Solution

  • If you aren't using the storyboard you don't need an identifier. Just instantiate and present it.

    let loadVC = SelectionScreen()
    self.present(loadVC, animated: true, completion: nil)