调用函数时,我希望SixthViewController
能够呈现给用户而无需用户按任何按钮。我知道如何通过设置按钮来显示另一个ViewController
来显示"显示"另一个ViewController
,但是一旦调用函数,我怎么能以编程方式强制SixthViewController
出现?
答案 0 :(得分:0)
你可以用这种方式呈现viewcontroller:
let destVC = SixthViewController()
self.present(destVC, animated: true, completion: nil)
或
let storyboard: UIStoryboard = UIStoryboard(name: "storyboardNamehere", bundle: nil)
let destVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerIdentifierhere" as! SixthViewController
self.present(destVC, animated: true, completion: nil)
=============================================== =========================
答案 1 :(得分:0)
如果您不使用segues但导航控制器请尝试:
let destinationVC = SixthViewController()
self.navigationController?.pushViewController(destinationVC, animated: true)
如果您只是想显示没有导航控制器:
let destinationVC = SixthViewController()
self.present(destinationVC, animated: true, completion: nil)
如果您使用情节提要:
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let destVC = self.storyboard?.instantiateViewController(withIdentifier: "SixthViewController" as! SixthViewController
self.present(destVC, animated: true, completion: nil)
答案 2 :(得分:0)
只需写下这个函数,并且当你想在func pushViewController之类的函数中推送或写下它时调用它,并将其称为self.pushViewController(),它就是它。
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let sixthViewController = storyBoard.instantiateViewController(withIdentifier: "SixthViewController") as! SixthViewController
self.navigationController?.pushViewController(sixthViewController, animated: true)