How to transition to a new view controller with code only using Swift

后端 未结 9 1520
半阙折子戏
半阙折子戏 2020-12-23 08:58

I want to transition from ViewController to secondViewController, when the user presses a UIButton, using code only in Swift.

//Function to transition
func          


        
9条回答
  •  一生所求
    2020-12-23 09:39

    SWIFT

    Usually for normal transition we use,

    let next:SecondViewController = SecondViewController()
    self.presentViewController(next, animated: true, completion: nil)
    

    But sometimes when using navigation controller, you might face a black screen. In that case, you need to use like,

    let next:ThirdViewController = storyboard?.instantiateViewControllerWithIdentifier("ThirdViewController") as! ThirdViewController
    self.navigationController?.pushViewController(next, animated: true)
    

    Moreover none of the above solution preserves navigationbar when you call from storyboard or single xib to another xib. If you use nav bar and want to preserve it just like normal push, you have to use,

    Let's say, "MyViewController" is identifier for MyViewController

    let viewController = MyViewController(nibName: "MyViewController", bundle: nil)
    self.navigationController?.pushViewController(viewController, animated: true)
    

提交回复
热议问题