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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:00:21+00:00 2026-05-17T21:00:21+00:00

I’m trying to unload a view controller from view when the iPhone goes to

  • 0

I’m trying to unload a view controller from view when the iPhone goes to sleep. My app has a stopwatch that has to keep counting when the phone goes to sleep or call comes in or the user closes the app without logging out.

I have all this functionality in place, I’m capturing all start times and stop times and upon re-entering the stopwatch view controller, I calculate the difference. It all works beautifully. When I was doing some additional testing I realised I hadn’t catered for the iPhone going into sleep mode.

So all I need to do to make sure my stopwatch is correct bring the user back to the app home screen. I know the following method is called when the app goes to sleep:

-(void)applicationWillResignActive:(UIApplication *)application

How do I unload the stopwatch view controller from my app delegate ?

—- UPDATE —-

kpower, thanks for your feedback. I’ve implemented the following code:

In my App Delegate:

- (void)applicationWillResignActive:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AppIsAsleep" object:nil];
}

In my view controller, I have the following:

-(void)viewDidLoad 
{   
    // Add Observer.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidUnload:) name:@"AppIsAsleep" object:nil];
}

- (void)viewDidUnload {
    //Remove the Observer.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AppIsAsleep" object:nil];
}

When the phone goes to sleep, it actually closes the app, am I doing something wrong ?

Regards,
Stephen

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

    You can use the Notifications mechanism. It allows you to unload view controller from different place (not the AppDelegate) this case.

    For example, in your view controller’s viewDidLoad method you add an observer (don’t forget to remove it in viewDidUnload) and in applicationWillResignActive: method of AppDelegate you just simply post notification. That’s all.

    ↓ Update here ↓

    When you get a notification – you should manage view controller’s removing by yourself. And calling viewDidUnload here is not the solution, cause this method is called after view controller was already unloaded and doesn’t cause removing.

    How to remove? Depends on how the view controller was added (for example, popViewControllerAnimated for UINavigationController). The main idea here is to make object’s retain count equal to 0 (as you know this case an object will be destroyed) – so you should sent release message necessary amount of times.

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

Sidebar

Related Questions

No related questions found

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.