以编程方式创建的UIButton转到其他VC,Swift

时间:2015-03-13 01:01:19

标签: swift

我以编程方式创建了一个UIButton。如何才能显示下一个View Controller?我希望它几乎与你控制时做的完全一样 - >拖动 - >在故事板中创建按钮时,模态到下一个View Controller。

到目前为止,这是我的代码。

let clickButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton

func clickPressed (sender: UIButton!) {
    //Presenting next View Controller happens here?        
}

func createClickButton () {    
    clickButton.frame = CGRect(x: 80 / 320 * view.frame.width,
        y: 204 / 568 * view.frame.height,
            width: 160 / 320 * view.frame.width,
                height: 160 / 568 * view.frame.height)
    if let clickImage = UIImage(named: "click") {
        clickButton.setImage(clickImage, forState: .Normal)
        clickButton.imageView?.contentMode = UIViewContentMode.ScaleAspectFit
    }

    clickButton.addTarget(self, action: "clickPressed:", forControlEvents: .TouchDown)
    clickButton.addTarget(self, action: "clickPressedUp:", forControlEvents: .TouchUpInside)
    self.view.addSubview(clickButton)
}

override func viewDidLoad() {
    super.viewDidLoad()
    createClickButton()
}

1 个答案:

答案 0 :(得分:1)

presentViewController(/*your view cntr here*/, animated: true, completion: nil)

例如,我有一个RecipeViewController。确保此视图控制器标有Storyboard ID。您可以在身份检查器(cmd + alt + 3)中执行此操作。此视图控制器的我的故事板ID是“RecipeViewController”。

let recipeViewController = storyboard!.instantiateViewControllerWithIdentifier("RecipeViewController") as RecipeViewController
presentViewController(recipeViewController, animated: true, completion: nil)