大佬教程收集整理的这篇文章主要介绍了iOS Swift – 如何以编程方式为所有按钮指定默认操作,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
根据UX准则,我想在应用程序中找到这些“非活动”按钮,并在测试期间点击时显示“功能不可用”警报.这可以通过扩展UIButton来完成吗?
class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() self.checkButtonAction() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) } @IBACtion func btn_Action(_ sender: UIButton) { } } extension UIViewController{ func checkButtonAction(){ for view in self.view.subviews as [UIView] { if let btn = view as? UIButton { if (btn.allTargets.isEmpty){ btn.add(for: .touchUpInside,{ let alert = UIAlertController(title: "Test 3",message:"No SELEctor",preferredStyle: UIAlertControllerStyle.alert) // add an action (button) alert.addAction(UIAlertAction(title: "OK",style: UIAlertActionStyle.default,handler: nil)) // show the alert self.present(alert,animated: true,completion: nil) }) } } } } } class ClosureSleeve { let closure: ()->() init (_ closure: @escaping ()->()) { self.closure = closure } @objc func invoke () { closure() } } extension UIControl { func add (for controlEvents: UIControlEvents,_ closure: @escaping ()->()) { let sleeve = ClosureSleeve(closurE) addTarget(sleeve,action: #SELEctor(ClosureSleeve.invokE),for: controlEvents) objc_setAssociatedObject(self,String(format: "[%d]",arc4random()),sleeve,objc_AssociationPolicy.oBJC_ASSOCIATION_RETAIN) } }
我测试了它.希望这可以帮助.快乐的编码.
以上是大佬教程为你收集整理的iOS Swift – 如何以编程方式为所有按钮指定默认操作全部内容,希望文章能够帮你解决iOS Swift – 如何以编程方式为所有按钮指定默认操作所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。