Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Open new View after click on Button

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

like image 651
dretker Avatar asked Jul 17 '17 08:07

dretker


People also ask

How do I link a button to another page in Xcode?

If you want to do this, right click drag (or control drag) from the button to the new view controller. This should make a grey line going from the first controller to the second one. You can then click on the little circle in the middle of the segue in interface builder to give it a name and specify the type.


2 Answers

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)
like image 137
Enamul Haque Avatar answered Sep 21 '22 23:09

Enamul Haque


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

like image 20
Kapil G Avatar answered Sep 24 '22 23:09

Kapil G