[英]Where to call a method to reload view on app return from background
在导航控制器的其中一个视图中,我有一个方法- (void) loadAlert
,该方法获取网页的内容,对其进行解析,然后将相关内容放置在UITextView
。 方法调用位于我的视图控制器的viewDidAppear:
部分。 在首次加载视图时以及在导航至视图时根本不显示视图时,它都可以正常工作。
我还希望当应用程序从后台返回时发生这种数据加载。 正如许多Stackoverflow帖子和其他来源指出的那样,我将我的方法调用放在applicationDidBecomeActive:
:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
MainViewController *mainView = [[mainViewController alloc] init];
[mainView loadAlert];
}
但是,当应用程序从后台返回时,视图中什么也没有发生。 我希望在从Internet获得内容时清除UITextView
,使UIActivityIndicatorView
旋转,并将新内容加载到UITextView
。
我知道该方法正在被调用,但是视图内什么也没有发生。 我的感觉是,在调用此方法时,视图尚未加载。 我在哪里可以放置我的方法调用,以便当应用程序从后台返回时,并且如果此特定视图是导航控制器中显示的当前视图,它将实际上刷新UI?
您可以使用UIApplicationWillEnterForegroundNotification
或UIApplicationDidBecomeActiveNotification
,在应用程序中的任何位置侦听这些通知是非常有效的,您需要将控制器注册为这些通知之一,并且仅使用您的方法显示所需的内容。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(showYourView)
name:UIApplicationWillEnterForegroundNotification object:nil];
-(void) showYouView(){
// do your stuff
}
注意
最好的方法是使用UIApplicationWillEnterForegroundNotification,因为它仅在用户从后台进入前台时才运行,另一方面,UIApplicationDidBecomeActiveNotification每次在您的应用程序处于活动状态时运行
您正在创建mainViewController
的新实例,而不是使用已经存在的相同实例。 每当您执行mainViewController *mainView = [[mainViewController alloc] init];
,这是一个新实例。 您应该尝试访问viewcontrollers堆栈中存在的相同实例,然后在该实例上调用此方法。
我在哪里可以放置我的方法调用,以便当应用程序从后台返回时,并且如果此特定视图是导航控制器中显示的当前视图,它将实际上刷新UI?
假设您已将UINavigationController添加为appdelegate中的属性,则可以在applicationWillEnterForeground
使用以下代码在topViewController上调用loadAlert。
UIViewController *aViewController = self.navigationController.topViewController;
if ([aViewController isKindOfClass:[mainViewController class]]) {
[(mainViewController *)aViewController loadAlert];
}
另外,请以大写字母开头课程名称。 例如:-您应该使用MainViewController
代替通常表示变量名称的mainViewController
。
更新:
如果使用的是UIApplicationWillEnterForegroundNotification
方法,则应在notificationMethod
方法中添加以上代码,以确保mainViewController
是可见的当前视图控制器。 否则,它可能会显示一些意外的行为。
所以像这样的东西应该很好:
- (void)notificationMethod {
UIViewController *aViewController = self.navigationController.topViewController;
if ([aViewController isKindOfClass:[mainViewController class]]) {
[(mainViewController *)aViewController loadAlert];
}
}
解决此问题的正确方法是让MainViewController
注册UIApplicationWillEnterForegroundNotification
。 然后,当收到通知时,您调用loadAlert
方法。
在MainViewController.m中:
- (void)enterForeground {
[self loadAlert];
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}
您无需为此在应用程序委托中做任何事情。 这样,MainViewController负责知道应该发生什么以及何时发生。
我对我的一个项目的需求完全相同,因此我必须通过NotificationCenter系统来处理此事。
首先,您需要在应用程序进入前台时启动通知:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"app_entering_foreground" object:self];
}
然后,您需要在需要的地方实际收听此特定通知。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUI) @"app_entering_foreground" object:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.