Swift - Open new View after click on Button

25,548

Solution 1

Your Segue doesn't look to have an identifier in the screenshot. Can you confirm. Please add an identifier to your segue from first view to second and then alter the below code with the name of segue identifier

performSegue(withIdentifier: "Segueidentifier", sender: self)} 

To add identifier on the segue, click on the segue in your storyboard and then on the right hand side in the Attributes Inspector add identifier in the Storyboard Segue section

Identifier

Solution 2

You can use storyboard Id

 let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "secondStoryboardId") as! SecondViewController
    self.present(nextViewController, animated:true, completion:nil)
Share:
25,548
Admin
Author by

Admin

Updated on July 17, 2020

Comments

  • Admin
    Admin almost 4 years

    I would like to open new View after clicking on a button. It works, when I make it by UIButton in Main.storyboard, however, I need to make it in code, because I have to use some if-statements with login/password. I tried to make it like it was suggested in other similar questions, but it doesn't work:

    @IBAction func login(_ sender: UIButton) {
    
       performSegue(withIdentifier: "Second", sender: self)} 
    

    Id of login-view: First;

    Id of the second view: Second

    enter image description here