LabelGestureRecognizer

LabelGestureRecognizer

我有2节课。第一个是Label,另一个是LabelGestureRecognizer。在LabelGestureRecognizer中,有一个名为getName的方法,我想通过Label类调用该方法。

以下是我的问题的简单示例。
我正在尝试将方法称为“getName”,但出现错误:
“'LabelGestureRecognizer'的不可见@interface声明选择器'getName:'”

ViewController.h:

@property (nonatomic, strong) UILabel *valueLabel;

ViewController.m:
- (void)viewDidLoad
{
    valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 366, 320, 50)];
    valueLabel.textAlignment = UITextAlignmentCenter;
    valueLabel.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:valueLabel];
}

- (void) wheelDidChangeValue:(NSString *) newValue {
    self.valueLabel.text = newValue;
}

Label.h:
@protocol LabelDelegate <NSObject>
@required
 -(void) wheelDidChangeValue:(NSString *) newValue;
@end

@class LabelGestureRecognizer;

LabelGestureRecognizer.m:
- (NSString *) getName:(int) position
{
    NSString *resultat = @"";
    switch (position) {
        case 0:
            resultat = @"Test 1";
            break;
        case 1:
            resultat = @"Test 2";
            break;
        default:
            break;
    }
    return resultat;
}

LabelGestureRecognizer.h:
 @interface LabelGestureRecognizer : UIGestureRecognizer
 {
     NSDate *previous;
     double current;
 }

Label.m:
 LabelGestureRecognizer *recognizer = [[LabelGestureRecognizer alloc] init];
 [self.delegate wheelDidChangeValue:[self getName:0]];

最佳答案

当您在self中使用Label.m时,表示当前的类对象。如果您需要表示LabelGestureRecognizer对象,则需要使用它代替self作为,

[self.delegate wheelDidChangeValue:[recognizer getName:0]];

另外,您还应在LabelGestureRecognizer.h文件中声明以下内容,
@interface LabelGestureRecognizer : UIGestureRecognizer
 {
     NSDate *previous;
     double current;
 }
- (NSString *) getName:(int) position;
@end

您的错误消息是说,它无法在getName类文件中看到LabelGestureRecognizer.h方法的声明。

关于iphone - 如何从iOS中的另一个类调用方法?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13341891/

10-12 00:42