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

后端 未结 9 1475
半阙折子戏
半阙折子戏 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:50

    For those using a second view controller with a storyboard in a .xib file, you will want to use the name of the .xib file in your constructor (without the.xib suffix).

    let settings_dialog:SettingsViewController = SettingsViewController(nibName: "SettingsViewController", bundle: nil)
    self.presentViewController(settings_dialog, animated: true, completion: nil)
    
    0 讨论(0)
  • 2020-12-23 09:53

    This worked perfectly for me:

    func switchScreen() {
        let mainStoryboard = UIStoryboard(name: "Main", bundle: Bundle.main)
        if let viewController = mainStoryboard.instantiateViewController(withIdentifier: "yourVcName") as? UIViewController {
            self.present(viewController, animated: true, completion: nil)
        }
    }
    
    0 讨论(0)
  • 2020-12-23 09:58

    In Swift 2.0 you can use this method:

        let registrationView = LMRegistration()
        self.presentViewController(registrationView, animated: true, completion: nil)
    
    0 讨论(0)
提交回复
热议问题