2011-05-11 62 views
4

我有一个问题,当应用程序从后台返回时,UIView的ViewWillAppear方法不会触发。这是一个问题,因为我的主应用程序屏幕显示的是从用户设置中检索的值,并且如果用户在应用程序在后台时更改了这些值,我需要刷新屏幕。我已经读过,应该使用NSNotificationCenter注册UIApplicationWillEnterForegroundNotification。如何在MonoTouch中注册UIApplicationWillEnterForegroundNotification

你如何在MonoTouch中做到这一点?或者是否有人知道确保屏幕始终保持最新状态的替代方法,即使从背景中返回时也是如此?

回答

7

你可以尝试沿着线的东西:

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

//snip 

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

请记住,你需要为你想从后台enterting何时更新每个视图控制器做到这一点!

+0

谢谢,卢克。 ViewWillAppear在应用程序从后台返回的情况下不会触发,这有点令人讨厌。但我会尝试你在这里提出的解决方案。谢谢。 – BruceHill 2011-05-11 16:30:20

+0

完美的作品!谢谢! :) – BruceHill 2011-05-11 20:33:51

+0

卢克,在这里使用NSNotificationCenter是不错的设计?重写AppDelegate中的相应方法,然后处理从那里的更新会不会更好?或者是NSNotifiactionCenters允许松耦合的论点? – Krumelur 2011-08-27 22:49:33