UITapGestureRecognizer sender 是手势,不是ui object
UITapGestureRecognizer sender is the gesture, not the ui object
我有一个叫的按钮,我给它一个 UIGestureRecognizer
,这样当长按按钮时 IBAction
就只有 运行。您可以通过向按钮本身添加 UILongPressGestureRecognizer
来实现。然后您控制将该手势识别器拖动到如下函数:
@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
出现,我希望标题显示按钮的当前标题。如果这个函数的发送者是按钮,我可以用 sender.currentTitle
来调用它。当功能的发送者是手势时,如何获得与手势相关的按钮的标题。
我在 objective C 上看到过这样的帖子,但在 Swift 上什么都没有。当您长按按钮本身时,项目将获得按钮描述,这就是全部。
您可以通过视图 属性 获取手势添加到的视图的引用。在这种情况下,您将它添加到按钮,因此视图 属性 将 return 您是按钮。
let button = sender.view as? UIButton
我有一个叫的按钮,我给它一个 UIGestureRecognizer
,这样当长按按钮时 IBAction
就只有 运行。您可以通过向按钮本身添加 UILongPressGestureRecognizer
来实现。然后您控制将该手势识别器拖动到如下函数:
@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
出现,我希望标题显示按钮的当前标题。如果这个函数的发送者是按钮,我可以用 sender.currentTitle
来调用它。当功能的发送者是手势时,如何获得与手势相关的按钮的标题。
我在 objective C 上看到过这样的帖子,但在 Swift 上什么都没有。当您长按按钮本身时,项目将获得按钮描述,这就是全部。
您可以通过视图 属性 获取手势添加到的视图的引用。在这种情况下,您将它添加到按钮,因此视图 属性 将 return 您是按钮。
let button = sender.view as? UIButton