我有一个被调用的按钮,我给了它一个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 上却没有看到过类似的帖子。这都是为了让项目在长按按钮本身时获取按钮描述。
您可以通过其 view 属性获取对手势添加到的视图的引用。在本例中,您将其添加到按钮中,以便视图属性将返回按钮。
let button = sender.view as? UIButton
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)