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

| 我试图找到一种识别按钮上的触摸并按住的方法。我以为对按钮进行子类化是一个好主意,但是我现在正为子类,parentsviews和viewcontroller的整个概念而苦苦挣扎。因此,请原谅,我担心这是一个初学者的问题: 如何从子类UIButton调用方法(已在ViewController中定义)? [self someMethod];不起作用-因为UIButton不是我的ViewController的后代。 [super someMethod];也不起作用-我想同样的问题 [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
    
已邀请:
        您只需在子类UIButton类中添加一个属性即可,该类包含视图控制器。进行初始化时,肯定需要添加控制器。     
        使用Objective-C的非常简单的委托概念。 在下面的文章中检查我的答案以在Objective-C中使用委托。 如何设置一个简单的委托以在两个视图控制器之间进行通信?     

要回复问题请先登录注册