如何在没有用户按任何按钮的情况下强制ViewController呈现?

时间:2017-08-03 16:10:48

标签: ios swift xcode

调用函数时,我希望SixthViewController能够呈现给用户而无需用户按任何按钮。我知道如何通过设置按钮来显示另一个ViewController来显示"显示"另一个ViewController,但是一旦调用函数,我怎么能以编程方式强制SixthViewController出现?

3 个答案:

答案 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)