45

I am currently showing a viewController in my new storyboard below:

var storyboard : UIStoryboard = UIStoryboard(name: AccountStoryboard, bundle: nil)
var vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"        
self.presentViewController(vc, animated: true, completion: nil)

However, this presents the viewcontroller without its embedded navigation controller. I tried changing "WelcomeID" to the navigation controller within the storyboard - however to no success.

I got this working in Objective -C, however don't know how to transform into swift:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SetupStoryboard" bundle:nil];
UINavigationController *navigationController1 = [storyboard instantiateInitialViewController];
navigationController1.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController1.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

WelcomeViewController *vc = (WelcomeViewController *)navigationController1.viewControllers[0];
vc.teststring = @"Hello";

[self presentViewController:navigationController1 animated:YES completion:nil];

How can you do this in swift?

Fudgey
  • 3,793
  • 7
  • 32
  • 53

5 Answers5

109

You're definitely on the right track. Unfortunately when you reference a view controller by its storyboard ID it will ignore the fact it is embedded within anything. Same goes for segues when you segue to something embedded, the destination view controller will be the embedding controller, not the controller you're usually interested in. Anyhow, you should be able to fix the problem in a similar way you've done in Objective-C, so this is just an exercise in syntax porting.

Edit: Define storyboard name with string now

let storyboard : UIStoryboard = UIStoryboard(name: "AccountStoryboard", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"        

let navigationController = UINavigationController(rootViewController: vc)

self.presentViewController(navigationController, animated: true, completion: nil)

OR you can give your embedding view controller an ID and instantiate that instead.

Joseph Astrahan
  • 8,659
  • 12
  • 83
  • 154
Chris Wagner
  • 20,773
  • 8
  • 74
  • 95
  • what if the destination ViewController isn't the direct rootView controller to the nav controller? I am getting the navigation pane but its theres no button back to the previous ViewController that its connected to. – shreddish Aug 18 '15 at 18:37
  • I'm not understanding the question. – Chris Wagner Aug 18 '15 at 22:51
  • I'm sorry I ended up figuring it out... I wanted to pop back to the root view controller of the navigation stack and then push to another view controller. I appreciate you getting back to me though – shreddish Aug 19 '15 at 14:05
  • how did you manage to get the back button on the instanciated controller? ==> answer: self.navigationController?.pushViewController(secondViewController, animated: true) – SKYnine Aug 28 '15 at 19:08
  • note: remember to always wrap the ViewController with NavigationController and only after that pass it to the method self.presentViewController(navigationController, animated: true, completion: nil) – kosiara - Bartosz Kosarzycki Sep 27 '15 at 22:38
20
 let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("WelcomeID") as SecondViewController

        self.navigationController?.pushViewController(secondViewController, animated: true)

Class Name is : SecondCiewController

Identifier Name

abdul sathar
  • 2,395
  • 2
  • 28
  • 38
14

The answer given by @Chris is works good in older version of swift.

Update Swift 3 & Swift 4

   let storyboard : UIStoryboard = UIStoryboard(name: "AccountStoryboard", bundle: nil)
   let vc : WelcomeViewController = storyboard.instantiateViewController(withIdentifier: "WelcomeID") as! WelcomeViewController
   vc.teststring = "hello"

   let navigationController = UINavigationController(rootViewController: vc)

   self.present(navigationController, animated: true, completion: nil)

Thanks!!!

Harjot Singh
  • 6,767
  • 2
  • 38
  • 34
0

Remember to setup the modalPresentationStyle property, in programmatically present the IB settings are ignored.

let sb = UIStoryboard(name: "retail_carrello", bundle: nil)
    guard let carrelloVC = sb.instantiateViewController(withIdentifier: "mainCarrello") as? retail_carrello else { return }

    let navController = UINavigationController(rootViewController: carrelloVC)

    navController.modalPresentationStyle = .fullScreen //<--- remember to set up this if you need fullscreen

     present(navController, animated: true, completion: nil)
Massimo Pavanel
  • 754
  • 8
  • 7
-1
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC

navigationController?.pushViewController(vc, animated: true)
EstevaoLuis
  • 2,422
  • 7
  • 33
  • 40
Aditya A.Rajan
  • 531
  • 5
  • 8