【问题标题】:How to call a method in the viewController from a subclassed UIButton?如何从子类 UIButton 调用 viewController 中的方法?
【发布时间】:2011-05-30 12:23:44
【问题描述】:

我试图找到一种方法来识别触摸并按住我的按钮。我认为子类化我的按钮是个好主意,但我现在正在为子类、parentviews 和 viewcontroller 的整个想法而苦苦挣扎。所以请原谅,我担心这是一个初学者的问题:

如何从子类 UIButton 调用方法(我已在 ViewController 中定义)?

  • [自我某种方法];不起作用 - 因为 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

【问题讨论】:

    标签: iphone objective-c cocoa-touch uibutton subclass


    【解决方案1】:

    您可以简单地在子类 UIButton 类中添加一个属性,该类包含视图控制器。初始化它时,您肯定需要添加控制器。

    【讨论】:

    • 非常感谢,但我该怎么做呢? @property(非原子,保留)ViewController MyViewController?我是否通过#importing 来添加控制器?
    • 您已经在子类 UIButton 中创建了属性。以相同的方式添加另一个 (UIViewController *myViewController),在初始化 Button 后,使用 ([yourbutton setMyViewController:self]; 添加 viewController;并使用 [self.myViewConroller yourMessage]; 在 UIButton 子类中调用它;
    • 再次感谢,但我无法正常工作。我现在收到错误消息“当我调用 [self.theViewController doSomething] 时访问未知的 theViewController getter 方法;
    • 另外,我收到警告说按钮(我在 viewController 中初始化)可能无法响应 setTheViewController...
    • 使用控制器类的名称代替 UIViewController。不要忘记导入头文件。
    【解决方案2】:

    使用 Objective-C 中非常简单的 delegate 概念。

    在下面的帖子中查看我在 Objective-C 中使用委托的答案。

    How do I set up a simple delegate to communicate between two view controllers?

    【讨论】:

    • 这是一种更简洁的方法。这不会将您的按钮绑定到您的视图控制器,并让您有机会在以后重用它。
    • @Deepak:谢谢……但我在试图理解这一点时感到头疼……这并不是我定义的那么简单。也许我需要更多时间来解决这个问题。目前,我坚持使用 unset 明显“不干净”的方法。
    • @n.evermind : 委托概念是在 Objective-C 编程中广泛使用的东西,如果你花点时间去理解它会更好,它会在很多情况下对你有所帮助。 .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多