5

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

Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45
dretker
  • 55
  • 1
  • 2
  • 8

6 Answers6

4

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)
Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
3

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

Kapil G
  • 4,081
  • 2
  • 20
  • 32
0

You shouldn't use storyboard ID for segues. There will be segue identifier separately. When you drag&drop segue you will get one line connected between the screens. Click on that to add segue identifier. You need to provide that segue ID here

You can see segue connection from the below image: enter image description here enter image description here

Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45
0

Set Segue identifier as below :

enter image description here

KKRocks
  • 8,222
  • 1
  • 18
  • 84
0

Select the segue and give a Identifier/Name ( Here , I've put toSecond) at Attribute Inspector and then perform segue .

ScreenShot : enter image description here

Source Code :

@IBAction func login(_ sender: UIButton) {

   performSegue(withIdentifier: "toSecond", sender: self)
} 
roy
  • 6,685
  • 3
  • 26
  • 39
-2

Also make sure that your segue is from the initial VC not the button...