Grumdrig
Grumdrig

Reputation: 16977

motionEnded not being called (no view controller)

I think I've done all I should to detect a shake, but motionEnded:withEvent: never gets called. (One wrinkle is that I don't have a UIViewController - my app is based on the "OpenGL ES App" template.)

I've added application.applicationSupportsShakeToEdit = YES; to my application:didFinishLaunchingWithOptions:, and

- (BOOL)canBecomeFirstResponder { return YES; }

to EAGLView.m (which does get called), and [self becomeFirstResponder]; to initWithCoder: (and have tried various other places too).

But the debugger never hits

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 

Am I missing some step? Do I have to have a controller?

(I'm using iOS 3.2 in the iPad simulator.)

Upvotes: 4

Views: 5365

Answers (4)

BadPirate
BadPirate

Reputation: 26177

The way the UIResponder chain works with the shake notification is obnoxious. Seems that UIWindow always gets the notification, and then sub-responders may or may not depending on whats above them in the chain. I created a UIWindow subclass, and defined it as my window class with the following:

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event 
{
    if (event.type == UIEventSubtypeMotionShake) 
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UIEventSubtypeMotionShakeEnded" object:nil];
}

Then, for any views that wanted the shake notifications, I simply had them add themselves as an observer to the UIEventSubtypeMotionShakeEnded event, and they got it every time.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(shakeNotification:)
                                             name:@"UIEventSubtypeMotionShakeEnded" object:nil];

Upvotes: 4

exeapps
exeapps

Reputation: 119

You must add this to your controller:

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

-(BOOL)becomeFirstResponder
{
    return YES;
}

Upvotes: 4

Grumdrig
Grumdrig

Reputation: 16977

Updating to the latest version of the SDK magically fixed the problem. [Shrug]

Upvotes: 0

tc.
tc.

Reputation: 33592

You can't become the first responder before your view is in the view hierarchy.

Upvotes: 0

Related Questions