第一个 UIButton 触发同一视图的另一个 UIbutton 事件
First UIButton fires another UIbutton event of same view
我在 swift 中创建了两个 UIButton 并以编程方式添加了操作。当我尝试点击第一个按钮时,两个按钮动作都会被触发。但是当我点击第二个按钮时 none 事件正在触发。
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")
}
}
更改此行
btnSort.addTarget(self, action: Selector("showFilterTbl:"), forControlEvents: UIControlEvents.TouchUpInside)
到
btnFilter.addTarget(self, action: Selector("showFilterTbl:"), forControlEvents: UIControlEvents.TouchUpInside)
我在 swift 中创建了两个 UIButton 并以编程方式添加了操作。当我尝试点击第一个按钮时,两个按钮动作都会被触发。但是当我点击第二个按钮时 none 事件正在触发。
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")
}
}
更改此行
btnSort.addTarget(self, action: Selector("showFilterTbl:"), forControlEvents: UIControlEvents.TouchUpInside)
到
btnFilter.addTarget(self, action: Selector("showFilterTbl:"), forControlEvents: UIControlEvents.TouchUpInside)