Ios 如何识别具有相同标记id的uibutton

Ios 如何识别具有相同标记id的uibutton,ios,swift,Ios,Swift,我在一个数组中循环,我使用索引来标记我的按钮id,但是当调用它们时,有1%的几率两个按钮标记id是相同的。当尝试使用相同的标记id调用其中一个按钮时,如何区分这两个按钮 let tmpButton = self.view.viewWithTag(MyVariables.SelectedChatLike) as! UIButton let path = NSBundle.mainBundle().pathForResource("myImage.gif", ofType: nil) tmpB

我在一个数组中循环,我使用索引来标记我的按钮id,但是当调用它们时,有1%的几率两个按钮标记id是相同的。当尝试使用相同的标记id调用其中一个按钮时,如何区分这两个按钮

 let tmpButton = self.view.viewWithTag(MyVariables.SelectedChatLike) as! UIButton
 let path = NSBundle.mainBundle().pathForResource("myImage.gif", ofType: nil)
 tmpButton.setImage(UIImage(contentsOfFile: path!), forState: UIControlState.Normal)

有时,上面的代码会更改错误的按钮图像,因为两个标记可能相同。简单回答:重构程序,使按钮标记ID始终唯一。我不能告诉你怎么做,因为你的描述太模糊了

顺便说一句,如果您的图像在捆绑包中,那么只需使用ImageName:

tmpButton.setImage(UIImage(named:"myImage.gif"), forState: .Normal)

如果您正在循环2组特定按钮的数组索引,请确保u add add,一组按钮为1000,另一组按钮为2000。 因此,这两组按钮将是标签[100010011002…]和[200020012002…]