如何从子类 UIButton 调用 viewController 中的方法?
我试图找到一种方法来识别按钮上的触摸和按住。我认为对我的按钮进行子类化是一个好主意,但我现在正在努力解决子类、parentsviews 和 viewcontroller 的整个想法。所以请原谅,我担心这是一个初学者的问题:
如何从子类 UIButton 调用方法(我在 ViewController 中定义)?
- [自我一些方法];不起作用 - 因为 UIButton 不是我的 ViewController 的后代。
- [超级一些方法];也不起作用 - 我想同样的问题
- [self.superview someMethod]; ...再次没有运气
- [MyViewController someMethod];也不起作用——因为它是“未声明的”——我必须导入我的 ViewController 吗?或者进行某种协议/类调用?
任何帮助将非常感激。
这是我的子类:
//
// MoleButton.h
//
#import <Foundation/Foundation.h>
@interface MoleButton : UIButton {
int page;
NSString *colour;
UIViewController *theViewController;
NSTimer *holdTimer;
}
@property (nonatomic, assign) int page;
@property (nonatomic, assign) NSString *colour;
@end
//
// MoleButton.m
#import "MoleButton.h"
@implementation MoleButton
@synthesize page, colour;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.superview touchesBegan:touches withEvent:event];
[holdTimer invalidate];
holdTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(touchWasHeld) userInfo:nil repeats:NO];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[self.superview touchesMoved:touches withEvent:event];
[holdTimer invalidate];
holdTimer = nil;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self.superview touchesEnded:touches withEvent:event];
}
- (void)touchWasHeld
{
holdTimer = nil;
// do your "held" behavior here
NSLog(@"TOUCH WAS HELD!!!!");
[self.theViewController doSomething];
}
@end
I was trying to find a way to recognise a touch&hold on my buttons. I thought that to subclass my buttons was a good idea, but I'm now struggling with the whole idea of subclasses, parentsviews and the viewcontroller. So please forgive, I fear that this is a beginner's question:
How do I call a method (which I've defined in my ViewController) from a subclassed UIButton?
- [self someMethod]; doesn't work - as UIButton is not a descendent of my ViewController.
- [super someMethod]; doesn't work either - same problem I suppose
- [self.superview someMethod]; ... again no luck
- [MyViewController someMethod]; doesn't work either -- as it is 'undecleared' -- do I have to import my ViewController? Or do some kind of protocol/class call?
Any help would be very much appreciated.
Here is my subclass:
//
// MoleButton.h
//
#import <Foundation/Foundation.h>
@interface MoleButton : UIButton {
int page;
NSString *colour;
UIViewController *theViewController;
NSTimer *holdTimer;
}
@property (nonatomic, assign) int page;
@property (nonatomic, assign) NSString *colour;
@end
//
// MoleButton.m
#import "MoleButton.h"
@implementation MoleButton
@synthesize page, colour;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
[self.superview touchesBegan:touches withEvent:event];
[holdTimer invalidate];
holdTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(touchWasHeld) userInfo:nil repeats:NO];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
[self.superview touchesMoved:touches withEvent:event];
[holdTimer invalidate];
holdTimer = nil;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
[self.superview touchesEnded:touches withEvent:event];
}
- (void)touchWasHeld
{
holdTimer = nil;
// do your "held" behavior here
NSLog(@"TOUCH WAS HELD!!!!");
[self.theViewController doSomething];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以简单地在子类 UIButton 类中添加一个属性,该类保存视图控制器。初始化时,当然需要添加控制器。
You can simply add a property in the subclassed UIButton class, which holds the view controller. When initializing it, you need to add the controller, for sure.
使用 Objective-C 非常简单的委托概念。
检查我在下面的帖子中关于在 Objective-C 中使用委托的答案。
如何设置一个简单的委托在两个视图控制器之间进行通信?
Use the very simple delegate concept of Objective-C .
Check my answer in the below post for using delegate in Objective-C .
How do I set up a simple delegate to communicate between two view controllers?