简体   繁体   English

点击时如何让UIButton执行我的类方法?

[英]How can I make a UIButton perform my class method when tapped?

How can I make a UIButton perform my class method, +doSomething:(id)sender , on class MyClass , after any event like UIControlEventTouchDown ? UIControlEventTouchDown类的任何事件之后,如何让UIButton在类MyClass上执行我的类方法+doSomething:(id)sender Can I use -addTarget:action:forControlEvents: like I do for instance methods? 我可以使用-addTarget:action:forControlEvents:就像我做的实例方法一样吗?

Thanks in advance. 提前致谢。

Since classes are also ObjC objects, you can use -addTarget:... as usual: 由于类也是ObjC对象,你可以像往常一样使用-addTarget:...

[button addTarget:[MyClass class]
           action:@selector(classMethod:)
 forControlEvents:UIControlEventTouchUpInside];

Add an in-between method that will call the Class method. 添加一个将调用Class方法的中间方法。

[button addTarget:self action:@selector(callClassMethod:) forControlEvents: UIControlEventTouchDown];

- (void)callClassMethod:(id)sender {
    [MyClass classMethod:sender];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在点击UIButton时将对象作为参数发送? - How to send an object as argument when a UIButton is tapped? 轻按UIButton时如何播放MP3? - how to play an MP3 when a UIButton is tapped? 如何控制点击UIButton为使用Xcode进行UI测试中的下一个动作做好准备之后的时间 - How can I control the time after an UIButton has been tapped to prepare for next action in UI Testing with Xcode 点击屏幕锁定按钮后,如何完全退出我的应用程序? - How can I completely exit my app when the screen lock button is tapped? 轻按uibutton时如何在视图控制器中加载新视图 - How to load a new view within a view controller when a uibutton is tapped Swift:如何在点击 UIButton 时打开新应用 - Swift: How to open a new app when UIButton is tapped 如何获取UIButton来调用目标类并挂钩此方法? (使用theos编译越狱调整) - How can I get UIButton to call target class and hook this method? (using theos to compile jailbreak tweak) 如何使用 Jqtouch 检测何时点击按钮? - How can i detect when a button is tapped using Jqtouch? 当我在按钮外触摸并在按钮上拖动时,如何使UIButton获得功能? - How can I make a UIButton get a function when I touch outside the button and drag over the button? 在点击时,UIButton在UItableview中创建单元格崩溃 - UIButton created in UItableview cell crashes when tapped
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM