如何从子类 UIButton 调用 viewController 中的方法?

发布于 2024-11-10 04:42:20 字数 1890 浏览 0 评论 0原文

我试图找到一种方法来识别按钮上的触摸和按住。我认为对我的按钮进行子类化是一个好主意,但我现在正在努力解决子类、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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱你一生 2024-11-17 04:42:20

您可以简单地在子类 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.

永不分离 2024-11-17 04:42:20

使用 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文