How do you register for UIApplicationWillEnterFore

2019-01-20 11:24发布

问题:

I have a problem that the ViewWillAppear method for a UIView does not fire when the application returns from the background. This is a problem, as my main application screen is showing values that are retrieved from the user settings, and if the user has changed these while the application was in the background I need to have the screen refreshed. I have read that one should register for the UIApplicationWillEnterForegroundNotification using NSNotificationCenter.

How do you do this in MonoTouch? Or does anyone know an alternate way of ensuring that the screen is always kept up to date, even when returning from the background?

回答1:

You could try something along the lines of:

//Register for the notification somewhere in the app
NSNotificationCenter.DefaultCenter.AddObserver(UIApplication.WillEnterForegroundNotification, EnteredForeground);

//snip

void EnteredForeground (NSNotification notification)
{
    // do your stuff here
}

Bear in mind you would need to do this for every view controller you'd like to update when enterting from the background!