我快速创建了两个 UIButton 并以编程方式添加了操作。当我尝试单击第一个按钮时,两个按钮操作都会被触发。但是当我点击第二个按钮时,没有任何事件被触发。
var btnSort = UIButton(frame: CGRectMake(2, 74, 140, 26))
btnSort.setTitle("SORT", forState: UIControlState.Normal)
btnSort.backgroundColor = UIColor.grayColor()
btnSort.tag=10
btnSort.addTarget(self, action: Selector("showSortTbl:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btnSort)
var btnFilter = UIButton(frame: CGRectMake(140+16+2, 74, 140, 26))
btnFilter.backgroundColor = UIColor.redColor()
btnFilter.tag=11
btnFilter.setTitle("FILTER", forState: UIControlState.Normal)
btnSort.addTarget(self, action: Selector("showFilterTbl:"), forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(btnFilter)
func showSortTbl(sender: UIButton){
var btnSendTag :UIButton = sender
if(btnSendTag.tag == 10){
println("show sort")
}
}
func showFilterTbl(sender: UIButton){
var btnSendTag :UIButton = sender
if(btnSendTag.tag == 11){
println("show filter")
}
}