0

I put the motionBegan:withEvent: method in my view controller to see if it would work, but it's not being called. In viewWillAppear, I made the view controller the first responder, and return YES from canBecomeFirstResponder.

This view controller is in the master pane of a split view, so maybe the first responder was changed before I shook the device.

But my real question is how can I have all motion events passed to a handler in my app delegate? Do I really have to have motionBegan:withEvent: methods in all view controllers or delegates to all first responders? Isn't there a way to events like this to flow up the responder chain without inserting code into each UIResponder? It seems that there would be, but I haven't put my finger on how to do this.

Jim
  • 5,940
  • 9
  • 44
  • 91

2 Answers2

1

the easiest way is override -sendEvent: in UIApplication then you will have controll over all events

that you may use this class

int retVal = UIApplicationMain(argc, argv, @"myApplication", @"myAppDelegate");

another solution is make category or expand -viewWillAppear: more info

Artur Gurgul
  • 396
  • 4
  • 21
  • Thanks for showing how the UIApplication class and UIApplicationMain function are tied to this. This is an area I was not familiar with, and it looks very useful to know about it.If I understand this right (and I'll try it tonight), then, in the subclassed UIApplication, I can intercept all events in `sendEvent:`, act on those I want to address immediately (like shake motion) and then call [super sendEvent:event] for those that should have the normal handling. – Jim May 14 '12 at 19:49
  • Subclassing UIApplication worked perfectly. For anyone else interested, shake events can occur even when the device is in the users' pocket. Some additional filtering will be needed. – Jim May 20 '12 at 22:12
0

What I've done in the pass is to create a category of UIWindow that implements a motionBegan method. In that motionBegan method, I broadcast a custom NSNotification. Then, anywhere in the application that wants motion events, just add an observer for your motion notification.

Since categories add methods to EVERY instance of an object, even system created windows get your motionBegan method, and do the right thing. It works beautifully.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • I'm not sure how this works if the UIWindow is not the delegate to the firstResponder. motionBegan: is a method in the delegate of the UIRespopnder (or subclassed UIResponder) object that is firstResponder. That may not always be the case (usually not, I'm betting.) Am I wrong? – Jim May 14 '12 at 19:52