Search code examples
iosswiftuistoryboardsegue

Creating a segue programmatically without a storyboard


Trying to create a segue for my register button which is going to send a user to a page that will allow them to register in my database.

Usually, I make a button and a target for my button which performs some action through a function.

Can we make this function segue PROGRAMMATICALLY mind you I haven't used storyboards at all?

Code Snippet:

 lazy var registerButton: UIButton = {

    let rButton = UIButton(type: .system)

     rButton.frame = CGRect(x: 210, y: 600, width: 100, height: 50)

    rButton.setTitle("REGISTER", for: .normal)
    rButton.backgroundColor = UIColor.white
    rButton.translatesAutoresizingMaskIntoConstraints = false
    rButton.setTitleColor(UIColor.black, for: .normal)
    rButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)

    rButton.layer.cornerRadius = 20
    rButton.layer.borderWidth = 2

    rButton.addTarget(self, action: #selector(regSegue), for: .touchUpInside)

    return rButton
}()

// will segue for me
func regSegue(){

}

Solution

  • I assume you meant a ViewController by the word page. So initialize your ViewController inside your func regSegue(){...} and present it using present(_:animated:completion:) method.

    Example:

    func regSegue(){
        let page = PageViewController()
        present(page, animated: true, completion: nil)
    }