So I have an application that is based on a TabBarController and in one of my tabs Im using the UIAccelerometer. The problem Im seeing is that I can access and use the accelerometer just fine until I switch tabs and switch back. Say for instance my accelerometer is on tab1, and I click tab2 and back to tab1 the accelerometer is off.
In my ViewWillAppear method Im setting the viewcontroller as firstresponder like so:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self becomeFirstResponder];
[accelerometer setDelegate:self];
...}
accelerometer is my instance variable for the viewcontroller. So I was reading in http://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/EventHandlingiPhoneOS.pdf this guide that if you set the accelerometers delegate to nil the phone will turn it off to conserve battery life. Noting that, I removed that line of code from my viewWillDisappear: method which is now pretty small:
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//commented out the line below and no dice
//[accelerometer setDelegate:nil];
if([[self tableView]isEditing])
[[self tableView]setEditing:NO];
[self resignFirstResponder];
}
Perusing SO and the net has led to little info on this. The only other info I can give here is that if I switch tabs back and forth, I can navigate backwards one page(its also got a navigationController and is the last page in the navigation) and forwards the accelerometer then works normally. It seems to me that its not really setting the view controller as the first responder. I would hope there is just something obvious Im not getting here. Any help would be much appreciated!!!
So it turns out that I needed to override viewDidAppear and viewDidDisappear methods in my viewController.
After doing this, the motionBegan was called after switching between viewControllers. Hope this helps anyone out there...