3

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(){

}
Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58
HiDungLing
  • 237
  • 4
  • 14

2 Answers2

4

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)
}
nayem
  • 7,285
  • 1
  • 33
  • 51
1

You can achieve this easily without a storyboard.First, you need to initialize the viewcontroller which you need to show.Then present the view controller.

 let vc: MyViewController = MyViewController()
 self.presentViewController(vc, animated: true, completion: nil)
Mudith Chathuranga Silva
  • 7,253
  • 2
  • 50
  • 58