我已創建5個按鈕動態斯威夫特這樣的:如何獲取UIButton ID?
for theIndex in 0..<5 {
let aButton = UIButton()
aButton.frame = CGRectMake(self.view.bounds.width/2 - 120, self.view.bounds.height/2 - 150, 240, 300)
aButton.setTitle("Button \(theIndex)", forState: .Normal)
aButton.addTarget(self, action: "btnClicked:", forControlEvents:.TouchUpInside)
self.view.addSubview(aButton)
}
但我怎麼能得到BUTTON_ID爲每一個?
func btnClicked(sender: AnyObject?) {
let vc = storyboard!.instantiateViewControllerWithIdentifier("vc_name") as! DetailVC
vc.selectedId = **WANT_TO_PASS_BUTTON_ID_HERE**
self.presentViewController(vc, animated:true, completion:nil)
}
考慮設置按鈕的標籤*或*將5個自定義按鈕的數組存儲在您的班級中。 – luk2302