0

HERE IS THE CODE: http://min.us/mWdMO0n14

I'm a Obj C newbie, so I've searched quite a bit, but haven't found anything that can solve my problem.

I have CalculatorViewController.h and .m and then CalculatorBrain.h and.m (Stanford Lectures)

in CalculatorBrain.m, I have the following method, with all of the variables defined as private in the CalculatorBrain header.

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

Then in CalculatorBrain.m , I have everything set up to detect shakes, as follows. I've included some of the code above the shake detection just so you have a general idea.

@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.

    }
}

I'm not sure how to call [brain clearEverythingOnShakeGesture]; , because I get the error "Class method +clearEverythingOnShakeGesture not found, defaults to return type id". However, if I make it a class method, the variables inside are instance variables, which provides another error. Any help greatly appreciated.

Alex
  • 103
  • 2
  • 8
  • Two suggestions: 1. make the method static. 2. use [self.brain clearEverythingOnShakeGesture]. – Liftoff Mar 25 '12 at 00:16
  • Did you try [self.brain clearEverythingOnShakeGesture]; ? To eliminate any ambiguity about what "brain" is I would synthesize it this way: @synthesize brain = _brain; . – StephenAshley.developer Mar 25 '12 at 00:19

2 Answers2

0

Are you #import-ing the CalculatorBrain.h? Also, you're using a nice lazy initialization pattern by building the CalculatorBrain in the getter, but you're not calling the getter in the motionBegan: method. Try [self.brain clearEverything ...] to get the brain instance.

I don't see anything in the code that would make the compiler think you have a class method. So that's mysterious. Please double check about the header import. You are correct that the clearEverything... should be an instance method.

danh
  • 62,181
  • 10
  • 95
  • 136
  • I've done what you said, and I no longer get the error. However, when I select the shake gesture in the simulator, the calculator does not clear itself. I know the method clearEverythingOnShakeGesture itself works, because when I call it through the use of a button, it works fine. – Alex Mar 25 '12 at 00:52
  • Are you saying motionBegan: is not called when you simulate the shake gesture? You'd see the SHAKE IT message in the log if it were. Have a look at http://stackoverflow.com/questions/150446/how-do-i-detect-when-someone-shakes-an-iphone to check your code for the shake. – danh Mar 25 '12 at 02:28
  • I see the message in the log, so I know the shake works. The problem is that nothing else works when the shake is detected. Here is the project if you would like to test it quickly. It would be greatly appreciated. http://min.us/mWdMO0n14 – Alex Mar 25 '12 at 02:52
  • @AlexG Looked at and fixed your project. Problem was nowhere near the shake gesture, but the way you had then main and cal nibs organized. Would like to post it for you on minus.com, but it doesn't let me upload folders. – danh Mar 25 '12 at 04:29
0

The project's AppDelegate posted in the comment above is building the calculator view controller from a nib, then releasing it immediately. The app functions partially, but the UILabel property to be cleared on the shake gesture is nulled at that point.

Also, it's a good practice to declare private properties in the private category, synthesize them with _underscore aliases, and refer to them as self.property outside of synthesized methods.

danh
  • 62,181
  • 10
  • 95
  • 136