Present View Controller in Storyboard with a Navigation Controller - Swift

后端 未结 5 1300
眼角桃花
眼角桃花 2021-01-30 10:30

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

var storyboard : UIStoryboard = UIStoryboard(name: AccountStoryboard, bundle: nil)
var vc : W         


        
相关标签:
5条回答
  • 2021-01-30 11:14
     let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("WelcomeID") as SecondViewController
    
            self.navigationController?.pushViewController(secondViewController, animated: true)
    

    Class Name is : SecondCiewController

    Identifier Name

    0 讨论(0)
  • 2021-01-30 11:20

    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.

    0 讨论(0)
  • 2021-01-30 11:20

    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)
    
    0 讨论(0)
  • 2021-01-30 11:20
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC
    
    navigationController?.pushViewController(vc, animated: true)
    
    0 讨论(0)
  • 2021-01-30 11:26

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

    0 讨论(0)
提交回复
热议问题