5

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....

Ariel
  • 2,471
  • 1
  • 26
  • 43
Ripa Saha
  • 2,532
  • 6
  • 27
  • 51

5 Answers5

8

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);
}
Abedalkareem Omreyh
  • 2,180
  • 1
  • 16
  • 20
1

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 -

Community
  • 1
  • 1
Anupam Mishra
  • 3,408
  • 5
  • 35
  • 63
1

Do simple way. Connect segue from firstViewController to secondViewController.Next click the segue goto show attribute inspector (top 3rd option from right side).In identifier give "your text". write below line into your button action. performSegueWithIdentifier("your text", sender: nil)

Pavankumar
  • 288
  • 1
  • 10
1

just use this , with code swift 2,3x,4x

if nib exist, let vc =

YourViewControllerName(nibName:"YourViewControllerName",bundle:nil)
    self.navigationController?.pushViewController(vc,animated:true)

if no nib

let vc = YourViewControllerName()
self.navigationController?.pushViewController(vc,animated:true)
Rishabh Shukla
  • 461
  • 6
  • 19
1

SWIFT 4

Based on @AbedalkareemOmreyh's answer:

  1. Go to Utilities, open identity inspector and enter the ID "MySecondSecreen" in the storyboard ID.
  2. Add the following code to your button:

    @IBAction func SaveTgw(sender: AnyObject) {
       let storyboard = UIStoryboard(name: "Main", bundle: nil);
       let vc = storyboard.instantiateViewController(withIdentifier: "MySecondSecreen")
                  self.present(vc, animated: true, completion: nil);
    }