Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6915993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:32:43+00:00 2026-05-27T09:32:43+00:00

I have setup some views with the navigation controller. I want to be able

  • 0

I have setup some views with the navigation controller. I want to be able to go back to the original view when I shake the phone. First, here’s the code snippet which I have in my AppDelegate:

- (void)startAccelerometerUpdates
{

[motionManager startDeviceMotionUpdatesToQueue:queue withHandler:^(CMDeviceMotion *motion, NSError *error) {
    // based from 
    // http://stackoverflow.com/questions/5214197/simple-iphone-motion-detect/5220796#5220796
    float accelerationThreshold = 1; // or whatever is appropriate - play around with   different values
    CMAcceleration userAcceleration = motion.userAcceleration;
    if (fabs(userAcceleration.x) > accelerationThreshold || 
        fabs(userAcceleration.y) > accelerationThreshold || 
        fabs(userAcceleration.z) > accelerationThreshold) {



        if(!self->isRoot) {
            NSLog(@"Stopping motion manager");
            [motionManager stopDeviceMotionUpdates];
            NSLog(@"popping to top view controller");
            [navcon popToRootViewControllerAnimated:YES];
            NSLog(@"Done popping");
        }

    }

}];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   .....

   motionManager = [[CMMotionManager alloc] init];
   motionManager.deviceMotionUpdateInterval = .5;

   isRoot = malloc(sizeof(BOOL));
   self->isRoot = NO;
   [self startAccelerometerUpdates];

   [navcon pushViewController:main animated:NO];
   ....
}

I was expecting that the call to popToRoot would be immediate. However, it takes around 30 seconds. Interesting to note that the button on top that you could press to manually go back to the previous page doesn’t work at this point. The phone seems to be doing a whole lot of work to freeze up the buttons. So, I’m definitely doing something wrong here.

It thought that maybe I keep calling the popToRootViewController several times so I added a check “isRoot” as you see in the code (please don’t distracted on why BOOL is a pointer, I have a reason for that).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:32:44+00:00Added an answer on May 27, 2026 at 9:32 am

    Your motion handler block appears to be intended to run on a background queue. UIKit methods (like popToRootViewController) should only be called on the main thread, and the behavior when you fail to follow that rule is frequently similar to what you have described.

    -performSelectorOnMainThread:withObject:waitUntilDone is the easiest way to make sure that your UIKit code runs on the main thread, but since -popToRootViewControllerAnimated: takes a non-object parameter, it takes a little more work. The simplest way is to add another method that takes no parameters:

    -(void)popToRootView {
        [navcon popToRootViewControllerAnimated:YES];
    }
    

    and then update your block to call that method on the main thread:

    if(!self->isRoot) {
        [motionManager stopDeviceMotionUpdates];
        [self performSelectorOnMainThread:@selector(popToRootView) withObject:nil waitUntilDone:NO];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some bindings that are setup as singletons. I want them to always
I have a navigation controller with a table view. In most tutorials I've read
I have an issue with some of my views, here is a breif outline
Hey all... I have a view controller (A) which on some action, alloc init's
I have some setup code in my functions.php file that sets permalinks and adds
I've created some MbUnit Test Fixtures that have SetUp methods marked with the SetUp
I have a setup function that runs onload to add some behaviours to elements.
I have a product setup executable that copies some files to the user's hard
My current setup : I have an entity object with some properties, among them
I have this following setup, a textarea named with some data in it that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.