4

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?

1
  • First, some questions: 1. Did you try to set the Storybard Identifier for your SelectionScreen like this? stackoverflow.com/a/48612127/1040347 2. Or do you want to avoid such approach? In this case you should create SelectionScreen in your 'clickMe' function like: let selectionScreen = SelectionScreen() and then perform present method. Please, give more details. Commented Sep 10, 2019 at 9:09

1 Answer 1

10

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)
Sign up to request clarification or add additional context in comments.

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.