Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open new screen on button click in swift

Tags:

ios

swift

I'm newbie in swift iOS. In my storyboard, I've created some dynamic fields from viewController. Have created a button from object library.

Now I need to open an new empty screen after clicking this button. In the new screen, there'll be some input fields. I'll create those through programming or object library.

But I'm unable to open the empty screen.

@IBAction func SaveTgw(sender: AnyObject) {
    print("test=====")

}

I need to include the screen opening code within SaveTgw. Any help or any descriptive tutorial link would be appreciated....

like image 780
Ripa Saha Avatar asked Aug 04 '16 05:08

Ripa Saha


2 Answers

1-From Utilities open identity inspector in storyboard ID enter id "MySecondSecreen"

enter image description here


2-In your method add the following code :

@IBAction func SaveTgw(sender: AnyObject) {
        let storyboard = UIStoryboard(name: "Main", bundle: nil);
        let vc = storyboard.instantiateViewControllerWithIdentifier("MySecondSecreen") as! UIViewController; // MySecondSecreen the storyboard ID
        self.presentViewController(vc, animated: true, completion: nil);
}
like image 99
Abedalkareem Omreyh Avatar answered Sep 28 '22 07:09

Abedalkareem Omreyh


Create a SecondViewController in Main.storyboard like this -

enter image description here On Scan button click , do following code

@IBAction func scanAction(sender: AnyObject) {
     let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)

let secondViewController = storyBoard.instantiateViewControllerWithIdentifier("SecondViewController") as SecondViewController
self.presentViewController(secondViewController, animated:true, completion:nil) 
}

There are many answers available, just do a search -

  • Swift: Navigate to new ViewController using button
  • Swift - Change view controller using action button
like image 45
Anupam Mishra Avatar answered Sep 28 '22 07:09

Anupam Mishra