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 7504903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:43:47+00:00 2026-05-29T21:43:47+00:00

I’m going to describe what I’m trying to do generally (in case there’s a

  • 0

I’m going to describe what I’m trying to do generally (in case there’s a better way) and then the stumbling block I’ve run into (in case my way is the best way).

What I want to do: I want to add an invocation to my undo manager that has a time limit. If the undo is not triggered within the time limit, it won’t be available when the device is shaken and thus nothing will happen.

What I’m doing: My approach was to use an NSUndoManager with an NSTimer. When I add the invocation to the undo manager, I kick off a 5-second timer as well. When the timer fires it checks !self.undoManager.isUndoing and if it’s true than it goes ahead and removes all actions from the undo manager. Testing it in the simulator works: a shake gesture kicks off the undo before 5 seconds, but not after.

The problem is that if I get a shake gesture under the 5 second limit, the undo manager shows the alert, but if the user waits until after the 5s limit to actually tap the undo button, nothing happens: the timer happily cleared the stack away, even though the alert view was visible.

Is there a way to check and see if the alert view is visible? Best would be if I could figure out if the user hit undo or cancel as well, and clear the undo manager’s action stack if the cancel button was pressed.

Or is there a better way besides using a timer in this manner?

Thanks!

Edited to add: My other thought was to capture the shake event myself (via the motionEnded:withEvent: call) and manually manage the alert and undo stack. Thoughts on this compared to the above are also welcome.

  • 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-29T21:43:51+00:00Added an answer on May 29, 2026 at 9:43 pm

    I ended up doing what I suggested in my edit—to use motionEnded:withEvent to manually manage the alert and undo. The downside to this is that you don’t get the built-in undo alert which has a slightly different style from a UIAlertView and enters the screen with a shaking motion.

    The upside is that I now have a undo that expires after 10 seconds. What follows is the general structure of the code in case you want the same.

    First, make sure your app can receive shake events and that you have an NSUndoManager you can access. You also need a timer; I have my code set up with an NSTimer that kicks off when the undoable event occurs and lasts 10 seconds. Make sure you add your undo target at the same timer your timer starts so that there’s actually something to undo.

    Next, implement motionEnded:withEvent like so:

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
        if (motion == UIEventSubtypeMotionShake && [self.undoManager canUndo]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Undo something?" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Undo", nil];
            [alert show];
            undoAlertIsVisible_= YES;
        }
    }
    

    I’m using an ivar called undoAlertIsVisible_ here to track if my alert is on screen.

    In your timer’s callback, do something like:

    if (!self.undoManager.isUndoing && !undoAlertIsVisible_) {
        // Clear away the possible undo
        [self.undoManager removeAllActionsWithTarget:self];
    }
    undoTimer_ = nil;
    

    Here we check to see we’re not currently undoing and the alert isn’t visible. If so, remove the undo actions and set the timer (another ivar) to nil. I’m setting the timer to nil so that I can check whether it’s fired in my alert callback, which is here:

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex != alertView.cancelButtonIndex) {
            if (self.undoManager.canUndo) {
                [self.undoManager undo];
            }
        }
        else {
            if (!undoTimer_) {
                // Timer fired while we were staring at the alert
                [self.undoManager removeAllActionsWithTarget:self];
            }
        }
        undoAlertIsVisible_= NO;
    }
    

    In the alert callback we either make the undo happen or, if the timer fired while the alert was visible and the alert was canceled, we clear possible undo actions. Otherwise the undo action would hang around after canceling with no timer to clear it.

    Hope this helps someone!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to select an H1 element which is the second-child in its group

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.