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



我以编程方式创建了一个UIButton。如何使其显示下一个视图控制器?我希望它在情节提要中创建按钮时,几乎可以执行与控制->拖动-> Modal到下一个视图控制器时完全相同的操作。

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

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()
}

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

例如,我有一个RecipeViewController。确保此视图控制器标记为情节提要 ID。您可以在身份检查器 (cmd + alt + 3) 中执行此操作。此视图控制器的情节提要 ID 是"RecipeViewController"。

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

最新更新