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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:37:17+00:00 2026-05-15T13:37:17+00:00

Apple recommends dismissing any UIAlertViews/UIActionSheets when entering background state in iOS 4. This is

  • 0

Apple recommends dismissing any UIAlertViews/UIActionSheets when entering background state in iOS 4. This is to avoid any confusion on the user’s part when he relaunches the application later. I wonder how I could elegantly dismiss all UIAlertViews at once, without retaining a reference to it everytime I set one up…

Any idea ?

  • 1 1 Answer
  • 3 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-15T13:37:18+00:00Added an answer on May 15, 2026 at 1:37 pm

    I was intrigued by Dad’s answer (funny username :), and curious why it was down-voted.

    So I tried it.

    Here is the .m part of a subclass of UIAlertView.

    Edit: (Cédric) I have added a way to catch calls to delegate methods and remove the observer then to avoid multiple registrations to the notification center.

    Everything bundled in a class in this github repo: https://github.com/sdarlington/WSLViewAutoDismiss

    
    
        #import "UIAlertViewAutoDismiss.h"
        #import <objc/runtime.h>
    
        @interface UIAlertViewAutoDismiss () <UIAlertViewDelegate> {
            id<UIAlertViewDelegate> __unsafe_unretained privateDelegate;
        }
        @end
    
        @implementation UIAlertViewAutoDismiss
    
        - (id)initWithTitle:(NSString *)title
                    message:(NSString *)message
                   delegate:(id)delegate
          cancelButtonTitle:(NSString *)cancelButtonTitle
          otherButtonTitles:(NSString *)otherButtonTitles, ...
        {
            self = [super initWithTitle:title
                                message:message
                               delegate:self
                      cancelButtonTitle:cancelButtonTitle
                      otherButtonTitles:nil, nil];
    
            if (self) {
                va_list args;
                va_start(args, otherButtonTitles);
                for (NSString *anOtherButtonTitle = otherButtonTitles; anOtherButtonTitle != nil; anOtherButtonTitle = va_arg(args, NSString *)) {
                    [self addButtonWithTitle:anOtherButtonTitle];
                }
                privateDelegate = delegate;
            }
            return self;
        }
    
        - (void)dealloc
        {
            privateDelegate = nil;
            [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
            [super dealloc];
        }
    
        - (void)setDelegate:(id)delegate
        {
            privateDelegate = delegate;
        }
    
        - (id)delegate
        {
            return privateDelegate;
        }
    
        - (void)show
        {
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                     selector:@selector(applicationDidEnterBackground:)
                                                         name:UIApplicationDidEnterBackgroundNotification
                                                       object:nil];
    
            [super show];
        }
    
        - (void)applicationDidEnterBackground:(NSNotification *)notification
        {
            [super dismissWithClickedButtonIndex:[self cancelButtonIndex] animated:NO];
            [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
        }
    
        #pragma mark - UIAlertViewDelegate
    
        // The code below avoids to re-implement all protocol methods to forward to the real delegate.
    
        - (id)forwardingTargetForSelector:(SEL)aSelector
        {
            struct objc_method_description hasMethod = protocol_getMethodDescription(@protocol(UIAlertViewDelegate), aSelector, NO, YES);
            if (hasMethod.name != NULL) {
                // The method is that of the UIAlertViewDelegate.
    
                if (aSelector == @selector(alertView:didDismissWithButtonIndex:) ||
                    aSelector == @selector(alertView:clickedButtonAtIndex:))
                {
                    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                                    name:UIApplicationDidEnterBackgroundNotification
                                                                  object:nil];
                }
                return privateDelegate;
            }
            else {
                return [super forwardingTargetForSelector:aSelector];
            }
        }
    
        @end
    
    

    It works nicely.
    It’s great, because you can just start using it the same way that you used to use UIAlertView.

    I haven’t had time to test it thoroughly, but I didn’t notice any side effect.

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

Sidebar

Related Questions

Apple recommends to declare a BOOL property this way: @property (nonatomic, assign, getter=isWorking) BOOL
Apple wants me to give the user a friendly apology if I can't find
From what I've read elsewhere, Apple recommends multiple versions of every graphical asset, so
In Apple documentation you can find that Apple recommends to release heavy data like
I know that Apple officially recommends UIKit to be used in the main thread
Apple's documentation recommends installing a keep-alive handler in the applicationDidEnterBackground: method. It also says
Apple recommends cropping out the status bar from screenshots submitted to the app store.
Apple recommends an itms:// link for native applications linking to the app store, however
If i save files inside the .app bundle, it saves OK, but apple recommends
Can I force a user to upgrade my app? Does Apple recommend it?

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.