這是一個重複的問題。我已經通過複製和粘貼到x代碼嘗試了幾個答案。然而我的按鈕操作從未被調用。迅速製作一個按鈕
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 50)
button.backgroundColor = UIColor.greenColor()
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents:UIControlEvents.TouchUpInside)
self.view.addSubview(button)
}
func buttonAction(sender:UIButton!) {
println("Button tapped") {
}
任何想法,爲什麼這是行不通的?
這是奇怪的,它看起來正確的給我。你能夠點擊UI中的按鈕嗎? – AdamPro13
看來我不能。我爲UIControlState.Highlighted添加了一個不同的標題,標題不變。還有其他方法我應該檢查嗎? – LeoReubelt
此外,我在xib中製作的按鈕在拖動創建IBAction後也不起作用。 – LeoReubelt