我有一个叫做的按钮,我给它一个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调用它.当函数的发送者是手势时,如何获得与手势相关联的按钮的标题.
我在目标C中看过这样的帖子,但在Swift上没有真正的帖子.当你长按按钮本身时,这就是项目获取按钮描述的全部内容.如果有人有任何想法如何用我到目前为止所做的完成,或者有不同的方法,请告诉我.
谢谢!
解决方法
您可以通过其视图属性获取对手势添加到的视图的引用.在这种情况下,您将其添加到按钮,以便view属性将返回给您按钮.
let button = sender.view as? UIButton
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。