UITapGestureRecognizer发件人是手势,而不是ui对象

我有一个button被调用,我给了它一个UIGestureRecognizer,所以IBAction只在长按button时运行。 您可以通过将UILongPressGestureRecognizer添加到buttoniteself来完成此操作。 然后,您可以控制将该手势识别器拖动到如下function:

@IBAction func handleGesture(sender: UIGestureRecognizer) { if sender.state == UIGestureRecognizerState.Began { let labelTap1=UITapGestureRecognizer(target: self, action: "labelTapped:") let alertController = UIAlertController(title: "**I want this to be the title of the button**", message: "This is the description of the function you pressed", preferredStyle: UIAlertControllerStyle.Alert) alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default,handler: nil)) self.presentViewController(alertController, animated: true, completion: nil) } } 

正如你所看到的UIAlertController出现,我希望标题显示button当前标题。 如果这个函数的发送者是button,我可以用sender.currentTitle调用它。 当函数的发送者是手势时,怎样才能得到与手势相关的button的标题。

我已经在目标C中看到类似这样的post,但没有真正在Swift上。 这是所有的项目,当你长按button本身时,得到一个button的描述。 如果有人有任何想法如何做到这一点,我已经做了什么,或者有不同的方法,请告诉我。

谢谢!

您可以通过其视图属性获取手势添加到的视图的引用。 在这种情况下,你将它添加到button,所以视图属性会返回给你你的button。

 let button = sender.view as? UIButton