在我的导航控制器中的一个视图中,我有一个方法- (void) loadAlert
,它可以获取网页的内容,对其进行解析,并将相关内容放在UITextView
上。方法调用位于视图控制器的viewDidAppear:
部分。它在视图首次加载时以及导航到视图时完全显示时工作正常。
我还希望在应用从后台返回时发生此数据加载。正如许多Stackoverflow帖子和其他来源所示,我将我的方法调用放在applicationDidBecomeActive:
:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
MainViewController *mainView = [[mainViewController alloc] init];
[mainView loadAlert];
}
但是,当应用从背景返回时,视图中不会发生任何事情。我希望UITextView
清除,UIActivityIndicatorView
旋转,同时从互联网获取内容,并将新内容加载到UITextView
。
我知道正在调用该方法,但视图中没有发生任何事情。我的感觉是,在调用此方法时,视图尚未加载。 在哪里可以调用我的方法,以便当应用程序从后台返回时,如果此特定视图是导航控制器中显示的当前视图,它实际上是否会刷新UI?
答案 0 :(得分:6)
您可以使用UIApplicationWillEnterForegroundNotification
或UIApplicationDidBecomeActiveNotification
,在应用程序的任何位置收听这些通知非常有效,您需要将控制器注册到其中一个通知中,然后使用您的方法显示无论你想要什么。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(showYourView)
name:UIApplicationWillEnterForegroundNotification object:nil];
-(void) showYouView(){
// do your stuff
}
注意强>
最好的方法是使用UIApplicationWillEnterForegroundNotification,因为它只会在用户从后台到前台运行时运行,另一方面UIApplicationDidBecomeActiveNotification会在每次应用程序生效时运行
答案 1 :(得分:2)
您正在创建mainViewController
的新实例,而不是使用已存在的同一实例。无论何时进行mainViewController *mainView = [[mainViewController alloc] init];
,它都是一个新实例。您应该尝试访问viewcontrollers堆栈中的相同实例,并在其上调用此方法。
我可以在哪里调用我的方法,以便在应用程序返回时 background,如果此特定视图是显示的当前视图 导航控制器,它实际上会刷新UI吗?
假设您已在appdelegate中将UINavigationController添加为属性,则可以使用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];
}
}
是当前可见的视图控制器。否则它可能表现出一些意想不到的行为。
所以这样的事情应该是好的:
{{1}}
答案 2 :(得分:2)
处理此问题的正确方法是让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];
}
您不需要在app委托中执行任何操作。这样,MainViewController负责知道应该发生什么以及何时发生。
答案 3 :(得分:0)
我对我的项目有完全相同的需求,我必须通过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];