0

Alright, so to get going with Objective-C (I'm usually just an HTML PhoneGap kinda guy), I made a simple Magic 8 ball app. I've got it right now so that when I touch the screen, the ball "shakes" and takes a random response and puts it in a label. What I want to do is when the iPhone itself is shaked, the text is updated too.

Here's my MainView.m:

#import "MainView.h"

@implementation MainView

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
 if (event.type == UIEventSubtypeMotionShake) {
  int rNumber = rand() % 26;
  switch (rNumber) {
                case 0:
                    shook.text = @"Never";
                    break;
            ....25 more entries.....
                default:
                    break;
           }
    }
 }

 - (IBAction)yesNo {
 [NSThread sleepForTimeInterval:0.75];
 int rNumber = rand() % 26;
 switch (rNumber) {
  case 0:
   result.text = @"Never";
   break;
  ........
  default:
  break;
 }
 }

 @end

and my MainView.h

 #import <UIKit/UIKit.h>
 #import <Foundation/Foundation.h>

 @interface MainView : UIView <UIAccelerometerDelegate> {
     IBOutlet UILabel *result;
  IBOutlet UILabel *shook;
 }

 - (IBAction)yesNo;
 - (void)motionEnded;

 @end     

Obviously there's an error in there, I know that much, but where?!

Jim
  • 72,985
  • 14
  • 101
  • 108
Peter Kazazes
  • 3,600
  • 7
  • 31
  • 60
  • Why is it obvious that there's an error in there? – Lily Ballard Jan 28 '11 at 00:21
  • 2
    Sleeping the main thread is a really bad idea. Why are you doing that? If you want to delay execution of some code, use `-performSelector:withObject:afterDelay:` instead. You should also refactor your random-testing code into a separate method that's called from both places. – Lily Ballard Jan 28 '11 at 00:22
  • Is the app crashing? If so, what does the log say? – WrightsCS Jan 28 '11 at 00:23
  • It's not crashing... the shake just isn't working. I'll change the sleeping to what you suggested. – Peter Kazazes Jan 28 '11 at 00:25
  • 1
    possible duplicate of [motionBegan: Not Working](http://stackoverflow.com/questions/1342674/motionbegan-not-working) – Jim Jan 28 '11 at 00:25

1 Answers1

1

It looks like you need to read the documentation in more depth to get to the bottom of this. Specifically the iOS Event Handling Guide.

From that document there are a few things which I would suggest you try:

  1. Override canBecomeFirstResponder: and return YES (as per Listing 4-1).
  2. Override viewDidAppear:animated: to become the first responder (as per Listing 4-1).
  3. Override both motionBegan:withEvent: and motionCancelled:withEvent: as they do this in the example which may be because the framework is testing your view controller class to see if it responds to these selectors (as per Listing 4-2).

Look at the UIResponder Class Reference (from which UIViewController inherits) for more detail on the methods you are overriding.

As someone who came from a Microsoft / VB / .NET and Android / Java background to Cocoa / Objective-C programming I would strongly suggest you invest the time in reading the documentation. Initially the Apple documents seem impenetrable but they're actually pretty good!

I hope that helps.

Quintin Willison
  • 610
  • 6
  • 13