2015-07-13 61 views
0
let signupButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton 
signupButton.setTranslatesAutoresizingMaskIntoConstraints(false) 
signupButton.setBackgroundImage(UIImage(named: "signupButton"), forState: .Normal) 
signupButton.setTranslatesAutoresizingMaskIntoConstraints(false) 
signupButton.setTitleColor(UIColor(red: 1, green: 1, blue: 1, alpha: 1), forState: .Normal) 
signupButton.setTitle(NSLocalizedString("SIGN UP", comment: ""), forState: .Normal) 

self.view.addSubview(signupButton) 
// width constraint 
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[SignupButton(==149)]", options: .DirectionLeadingToTrailing, metrics: nil, views: ["SignupButton": signupButton])) 

// height constraint 
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[SignupButton(==60)]", options: .DirectionLeadingToTrailing, metrics: nil, views: ["SignupButton": signupButton])) 

self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-464.5-[signupButton]-142.5-|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["signupButton": signupButton])) 
self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-196-[signupButton]-30-|", options: .DirectionLeadingToTrailing, metrics: nil, views: ["signupButton": signupButton])) 

當按下此按鈕時,我需要它實質上推送到另一個ViewController。按下按鈕到下一個「故事板」以編程方式在Swift中設計用戶界面

回答

1

必須使用代碼

signupButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside) 

然後創建一個名爲函數把按鈕動作按下

func pressed (sender:UIButton!) { 

//write the code for changing view controller 

} 
+1

作爲一個額外的建議,考慮到問題的標題,我建議@ vkrishnan23調查'performSegueWithIdentifier(identifier:String?,sender:AnyObject?)' –

+0

是的,但是這會導致SIGABRT App Delegate錯誤。這是我聲明signupButton.addTarget的問題,因爲它是在子視圖之前聲明的。 – vkrishnan23

+0

@MatteoPiombo問題在於,我以編程方式設計用戶界面,因此無法創建segues,因爲這些都是通過storyboard,可以使用performSeguewithIdentifier調用。 – vkrishnan23

相關問題