简体   繁体   English

在哪里调用从后台返回应用时重新加载视图的方法

[英]Where to call a method to reload view on app return from background

In one of my views in a navigation controller, I have a method - (void) loadAlert that gets the content of a web page, parses it, and places the relevant content on a UITextView . 在导航控制器的其中一个视图中,我有一个方法- (void) loadAlert ,该方法获取网页的内容,对其进行解析,然后将相关内容放置在UITextView The method call is located in the viewDidAppear: section of my view controller. 方法调用位于我的视图控制器的viewDidAppear:部分。 It works fine when the view first loads and when the view appears at all when navigated to. 在首次加载视图时以及在导航至视图时根本不显示视图时,它都可以正常工作。

I also want this data load to occur when the app returns from background. 我还希望当应用程序从后台返回时发生这种数据加载。 As many Stackoverflow posts and other sources indicated, I put my method call in applicationDidBecomeActive: : 正如许多Stackoverflow帖子和其他来源指出的那样,我将我的方法调用放在applicationDidBecomeActive:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    MainViewController *mainView = [[mainViewController alloc] init];
    [mainView loadAlert];
}

However, nothing occurs within the view when the app returns from background. 但是,当应用程序从后台返回时,视图中什么也没有发生。 I expect the UITextView to clear, a UIActivityIndicatorView to spin while the content is obtained from the Internet, and the new content to be loaded into the UITextView . 我希望在从Internet获得内容时清除UITextView ,使UIActivityIndicatorView旋转,并将新内容加载到UITextView

I know that the method is being called, but nothing is happening within the view. 我知道该方法正在被调用,但是视图内什么也没有发生。 My feeling is that the view has not yet loaded when this method is being called. 我的感觉是,在调用此方法时,视图尚未加载。 Where can I put my method call so that when the app returns from background, and if this specific view is the current view displayed in the navigation controller, it will actually refresh the UI? 我在哪里可以放置我的方法调用,以便当应用程序从后台返回时,并且如果此特定视图是导航控制器中显示的当前视图,它将实际上刷新UI?

you can use UIApplicationWillEnterForegroundNotification or UIApplicationDidBecomeActiveNotification , it is quite effective to listen to these notifications anywhere in your application , you need to register your controller to one of these notification and just use your method to show whatever you want to. 您可以使用UIApplicationWillEnterForegroundNotificationUIApplicationDidBecomeActiveNotification ,在应用程序中的任何位置侦听这些通知是非常有效的,您需要将控制器注册为这些通知之一,并且仅使用您的方法显示所需的内容。

     [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(showYourView)
                                                 name:UIApplicationWillEnterForegroundNotification object:nil];

-(void) showYouView(){
 // do your stuff
}

NOTE 注意

Best way is to use UIApplicationWillEnterForegroundNotification because it will only run when user comes from background to foreground, on the other hand UIApplicationDidBecomeActiveNotification runs every time your application become active 最好的方法是使用UIApplicationWillEnterForegroundNotification,因为它仅在用户从后台进入前台时才运行,另一方面,UIApplicationDidBecomeActiveNotification每次在您的应用程序处于活动状态时运行

You are creating a new instance of mainViewController rather than using the same instance which is already there. 您正在创建mainViewController的新实例,而不是使用已经存在的相同实例。 Whenever you are doing a mainViewController *mainView = [[mainViewController alloc] init]; 每当您执行mainViewController *mainView = [[mainViewController alloc] init]; , it is a new instance. ,这是一个新实例。 You should try to access the same instance which is there in the viewcontrollers stack and call this method on that. 您应该尝试访问viewcontrollers堆栈中存在的相同实例,然后在该实例上调用此方法。

Where can I put my method call so that when the app returns from background, and if this specific view is the current view displayed in the navigation controller, it will actually refresh the UI? 我在哪里可以放置我的方法调用,以便当应用程序从后台返回时,并且如果此特定视图是导航控制器中显示的当前视图,它将实际上刷新UI?

Assuming that you have your UINavigationController added as a property in your appdelegate, you can use the below code in applicationWillEnterForeground to call loadAlert on the topViewController. 假设您已将UINavigationController添加为appdelegate中的属性,则可以在applicationWillEnterForeground使用以下代码在topViewController上调用loadAlert。

UIViewController *aViewController = self.navigationController.topViewController;
if ([aViewController isKindOfClass:[mainViewController class]]) {
    [(mainViewController *)aViewController loadAlert];
}

On a side note, please start class names with capital letter. 另外,请以大写字母开头课程名称。 For eg:- you should have used MainViewController instead of mainViewController which normally represents a variable name. 例如:-您应该使用MainViewController代替通常表示变量名称的mainViewController

Update: 更新:

If you are using the UIApplicationWillEnterForegroundNotification approach, you should add the above code in notificationMethod method to make sure that mainViewController is the current view controller which is visible. 如果使用的是UIApplicationWillEnterForegroundNotification方法,则应在notificationMethod方法中添加以上代码,以确保mainViewController是可见的当前视图控制器。 Else it could show some unexpected behaviors. 否则,它可能会显示一些意外的行为。

So something like this should be good: 所以像这样的东西应该很好:

- (void)notificationMethod {

   UIViewController *aViewController = self.navigationController.topViewController;
   if ([aViewController isKindOfClass:[mainViewController class]]) {
      [(mainViewController *)aViewController loadAlert];
   }
}

The proper way to handle this is to have your MainViewController register for the UIApplicationWillEnterForegroundNotification . 解决此问题的正确方法是让MainViewController注册UIApplicationWillEnterForegroundNotification Then when the notification is received, you call the loadAlert method. 然后,当收到通知时,您调用loadAlert方法。

In MainViewController.m: 在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];
}

You do not need to do anything in the app delegate for this. 您无需为此在应用程序委托中做任何事情。 This way, the MainViewController is responsible for know what should happen and when. 这样,MainViewController负责知道应该发生什么以及何时发生。

I had the exact same need for a project of mine, and I had to go through the notificationCenter system to handle the matter. 我对我的一个项目的需求完全相同,因此我必须通过NotificationCenter系统来处理此事。

First, you need to launch the notification when your application is entering foreground : 首先,您需要在应用程序进入前台时启动通知:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"app_entering_foreground" object:self];
}

Then you need to actually listen to this particular notification where you need it. 然后,您需要在需要的地方实际收听此特定通知。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUI) @"app_entering_foreground" object:nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM