简体   繁体   English

如何在 Swift 2.0 中没有 Storyboard 的情况下以编程方式从一个 ViewController 导航到另一个

[英]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首先添加按钮 TouchUpInside-event 并将其绑定到选择器

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

Then implement the method "pressed", like this:然后实现方法“pressed”,如下所示:

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

This assumes that your ViewController is in a NavigationController!这假设您的 ViewController 在 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
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有情节提要ID的情况下迅速为导航控制器设置根视图控制器。 并导航到其他视图,仅使用viewcontroller名称 - How to set root view controller for a navigation controller in swift without storyboard id. And navigate to other view Only with viewcontroller name 如何从 viewcontroller.swift 文件导航到 *.storyboard 文件中的相应场景? - How navigate from viewcontroller.swift file to corresponding scene in *.storyboard file? 如何以编程方式快速创建 uicollectionview(没有故事板) - how to create uicollectionview with swift programatically (without storyboard) 如何从一个故事板导航到另一个 TabBarController? - How to navigate from one storyboard to another TabBarController? 从UITabviewController导航回到情节提要中的ViewController - Navigate back from UITabviewController to ViewController in storyboard Swift 4.2:如何在 ViewController 中嵌入 UIPageControl(没有 Storyboard)? - Swift 4.2: How to embed UIPageControl in ViewController (without Storyboard)? 如何在Ios中从一个项目情节提要导航到另一个项目情节提要 - How to navigate from one project storyboard to another project storyboard in Ios Swift 从第三个 ViewController 导航到第一个 ViewController - Swift Navigate from third ViewController to the first ViewController 如何在 Storyboard 中将 ViewController.swift 连接到 ViewController? - How to connect ViewController.swift to ViewController in Storyboard? 如何在 swift UI 中单击按钮时从 swift UI 导航到 storyboard? - How to navigate from swift UI to storyboard on button click in swift UI?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM