简体   繁体   中英

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 . The method call is located in the viewDidAppear: section of my view controller. 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: :

- (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 .

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?

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.

     [[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

You are creating a new instance of mainViewController rather than using the same instance which is already there. Whenever you are doing a 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.

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?

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.

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.

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. 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 . Then when the notification is received, you call the loadAlert method.

In 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.

I had the exact same need for a project of mine, and I had to go through the notificationCenter system to handle the matter.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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