I am working on an app that needs to update the UI based on the current date when it is loaded. I told it to do the update in my viewWillAppear, but about half the time it would not update when I loaded the program. Shouldn’t viewWillAppear always be called when your view is about to appear? Apparently not, but I’ll tell you how I got the functionality I was looking for.
Updating from the background with Notifications
So, viewWillAppear will run every time your view is loaded from disk, or being called to be onscreen by a segue from another view. However, when I leave the app, use some other apps and come back, as far as my app was concerned, this view never left. It left as far as the system was concerned, but in my app’s little world, it just had the same view up as it did when I left the app. When I left the app, I sent it to the background. That is the difference.
So, how do I update my screen when it returns from the background? There are two notifications that would be of help here. They are:
- UIApplicationWillEnterForegroundNotification
- UIApplicationDidBecomeActiveNotification
As your program is brought back from the background, these two notifications are raised, in that order. UIApplicationWillEnterForegroundNotification will run right before the view should appear onscreen, and UIApplicationDidBecomeActiveNotification should run just after. In my previous post about Supporting Dynamic Type, I mentioned how to subscribe to notifications, but for simplicity’s sake, I will show you here.
I added this to my viewWillAppear:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateGUI)
name:UIApplicationWillEnterForegroundNotification
object:[UIApplication sharedApplication]];
Then, in a new method updateGUI, just do whatever calculations or updates you need to do before the view comes back from the background. In iOS7, the system takes a screenshot of your view when it is sent to the background, and shows that first when you load your app again, and then runs your code appropriately. So even though we put this on the notification for before the view is visible, we still may see the previous value for a moment before the update is called. I believe there are some ways to update the screenshot in the background every so often, but I do not know how yet. I will look further in to it, but this still nonetheless fixed my original problem of only occasionally changing the value, and having to forcefully close the program and rerun it, or do some other action to call updateGUI. I’m just taking this one step at a time.
Cleaning up NSNotification Center afterwards
And of course, we should clean up after ourselves, so again we will just unsubscribe from all notifications when we disappear.
-(void) viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
And that’s all there is to it. I know this post is a bit shorter than usual, but I felt this was a useful tip to share. I was confused about a problem I had, discovered how to fix it, and thought others might find the solution helpful. If you have any questions, don’t hesitate to contact me on twitter @CodingExplorer, and I’ll see what I can do.
Thanks!