简体   繁体   中英

How to navigate from one ViewController to Other Programatically, without Storyboard in Swift 2.0

I have a button which is located on superview:

let buttonSignUp = UIButton()
self.view.addSubview(buttonSignUp)

When I press this button I want to go to another view controller scene.

First add the buttons TouchUpInside-event and bind it to a selector

    buttonSignUp(self, action: "pressed:", forControlEvents: .TouchUpInside)

Then implement the method "pressed", like this:

func pressed(sender: UIButton!) {
    let vc = UIViewController()
    self.navigationController?.pushViewController(vc, animated: true)
}

This assumes that your ViewController is in a NavigationController!

If you instead want to present the view modally, you can do this:

 let vc = UIViewController()
 self.presentViewController(vc, animated: true) { () -> Void in
        //Here you can write code that you want to run WHEN the modal present is completed
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM