Presenting a view controller programmatically in swift

隐身守侯 提交于 2019-12-03 06:39:06

Do you want to present navController modally?

if yes, this is the answer

self.presentViewController(navController, animated: true, completion: nil)

"self" is the current view controller that will present the navController

And put it like this,

class ViewController: UIViewController {       

    override func viewDidLoad() {
        super.viewDidLoad()

        var theButton = UIButton()

        // Add the event to button
        theButton.addTarget(self, action: "buttonTouchInside:", forControlEvents: .TouchUpInside)

        self.view.addSubview(theButton)
    }

    func buttonTouchInside(sender:UIButton!)
    {
        // When the button is touched, we're going to present the view controller

        // 1. Wrap your view controller within the navigation controller

        let navController = UINavigationController(rootViewController: yourViewController)

        // 2. Present the navigation controller

        self.presentViewController(navController, animated: true, completion: nil)
    }

}

But,

If you want to navigate between viewController in the navigationController, you can use

self.navigationController.pushViewController(viewControllerToPush, animated: true)
A.G

I made a simple solution. Here it is..

func actioncall () {
    let loginPageView =  self.storyboard?.instantiateViewControllerWithIdentifier("LoginPageID") as! ViewController
    self.presentViewController(loginPageView, animated: true, completion: nil)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!