0

这是代码:http: //min.us/mWdMO0n14

我是 Obj C 新手,所以我搜索了很多,但没有找到任何可以解决我的问题的东西。

我有 CalculatorViewController.h 和 .m,然后是 CalculatorBrain.h 和.m(斯坦福讲座)

在 CalculatorBrain.m 中,我有以下方法,所有变量在 CalculatorBrain 标头中定义为私有。

- (void)clearEverythingOnShakeGesture{
    operand = 0;
    waitingOperation = @"";
    waitingOperand = 0;
}

然后在 CalculatorBrain.m 中,我将一切设置为检测抖动,如下所示。我在抖动检测上方包含了一些代码,只是为了让您有一个大致的了解。

@interface CalculatorViewController()
@property(nonatomic, retain) CalculatorBrain *brain;
@end

@implementation CalculatorViewController

@synthesize brain;
- (CalculatorBrain *)brain {
    if (!brain) {
        brain = [[CalculatorBrain alloc] init];
    }
    return brain;
}

-(BOOL)canBecomeFirstResponder{
    return YES;
}

-(void)viewDidAppear: (BOOL) animated{
    [super viewDidAppear:animated];
    [self becomeFirstResponder]; 
}

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (event.subtype == UIEventSubtypeMotionShake)
    {
        NSLog(@"SHAKE IT!");
        [brain clearEverythingOnShakeGesture]; //********** not sure how to call this.

    }
}

我不确定如何调用[brain clearEverythingOnShakeGesture];,因为我收到错误“找不到类方法 +clearEverythingOnShakeGesture,默认返回类型 id”。但是,如果我将其设为类方法,则其中的变量是实例变量,这会产生另一个错误。非常感谢任何帮助。

4

2 回答 2

0

你在#import-ing CalculatorBrain.h 吗?此外,您通过在 getter 中构建 CalculatorBrain 使用了一个很好的惰性初始化模式,但您没有在 motionBegan: 方法中调用 getter。尝试 [self.brain clearEverything ...] 获取大脑实例。

我在代码中看不到任何会让编译器认为你有类方法的东西。所以这很神秘。请仔细检查标题导入。你是正确的 clearEverything... 应该是一个实例方法。

于 2012-03-25T00:26:24.130 回答
0

上面评论中发布的项目 AppDelegate 是从一个 nib 构建计算器视图控制器,然后立即发布它。该应用程序部分功能,但要在摇动手势上清除的 UILabel 属性此时为空。

此外,在私有类别中声明私有属性,使用 _underscore 别名合成它们,并在合成方法之外将它们称为 self.property 是一种很好的做法。

于 2012-03-25T14:24:57.213 回答