I am trying to add labels to Swift that are being added to the loop. Then I want to add the โTapGestureโ event for each of them as I add. It works, but the problem is that the called function takes the data from the label for use when pressed, but the label was overridden by that time, and it takes data from the last one added, and not the one that was pressed. How can I make everyone unique?
self.label.attributedText = self.myMutableString let tapGesture = UITapGestureRecognizer(target: self, action: handleTap(label)) self.label.userInteractionEnabled=true self.label.addGestureRecognizer(tapGesture) self.label.font = UIFont.boldSystemFontOfSize(28) self.label.sizeToFit() self.label.center = CGPoint(x: screenWidth, y: top) if(kilom>=30||self.located==false){ self.scroller.addSubview(self.label) if(device=="iPhone"||device=="iPhone Simulator"){ top = top+80 } else{ top = top+140 } }
The following is the gesture recognizer code that receives and uses tag data:
func handleTap(sender:UILabel){ var a = self.label.text let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("displayer") self.presentViewController(resultViewController, animated: true, completion: nil) }
uilabel swift addtarget
gareth power
source share