如何以编程方式从一个ViewController导航到其他,在Swift 2.0中没有Storyboard

时间:2016-03-17 10:59:15

标签: ios swift button

我有一个位于superview的按钮:

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

当我按下此按钮时,我想转到另一个视图控制器场景。

1 个答案:

答案 0 :(得分:0)

首先添加按钮TouchUpInside-event并将其绑定到选择器

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

然后实现“按下”方法,如下所示:

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

这假设您的ViewController在NavigationController中!

如果您想要以模态方式呈现视图,可以执行以下操作:

 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
 }