I'm pretty new to iPhone development. I have my "root" view and it is implementing
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
This implementation is working just fine. After a user pushes a button, I do a pushViewController to push a new view onto the stack. If the user shakes the iPhone, the a开发者_开发知识库ccelerometer fires even though the currently "popped" view is not implementing any accelerometer related methods. It appears to me that the root view is still active even though a different view has been pushed onto the stack. Why is a view that's not the active view still responding to accelerometer?
Thanks
Tom
Actually the root view controller is not inactive in this case. It's just not visible. It is still alive and can receive events.
In your accelerometer delegate, have a flag to check if the root view is visible. Do any operations only if the root view is visible.
if (visible) {
// Do operations
}
I think the accelerometer doesn't know anything about active or inactive view controllers.
The only thing you do is to set your ViewController as a delegate (to receive events) to the accelerometer. After pushing another ViewController onto the stack your origin ViewController continues to exist. And so it continues to receive events.
You have to deal with this on your own - eg. by checking a local variable or (but not tested) by checking the window property of the view.
if (self.view.window!=nil) { // viewController is active }
精彩评论