Pregunta

I'm getting data off the server via JSON and displaying it on Labels. I've added that method in viewDidLoad.

I want to refresh the data when the user opens the app again. Currently, even if I kill the app in the simulator and start the app again, it doesn't refresh.

I tried the viewDidAppear method, but it isn't being executed for some reason.

-(void)viewDidAppear{
        NSLog(@"Called viewDidAppear");
}

This is never called. I tried to minimize the app but it didn't work.

¿Fue útil?

Solución 2

I followed this tutorial - http://leejon.es/notifying-a-viewcontroller-with-uiapplicationdidbecomeactivenotification/

First, attach to the notification in the viewWillAppear method of the target view controller:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector( appActivated: )
                                                 name: UIApplicationDidBecomeActiveNotification
                                               object: nil];

}


- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self ];
}


- (void)appActivated:(NSNotification *)note
{
    [self update];
}

Otros consejos

You can listen for notifications and respond appropriately. Try using these and decide what works for your intended workflow.

UIApplicationDidBecomeActiveNotification
UIApplicationWillEnterForegroundNotification

You can use respond to the notification like this.

[[NSNotificationCenter defaultCenter] addObserverForName: UIApplicationDidBecomeActiveNotification object: nil queue: [NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
    // LOAD JSON
}];

The viewDidAppear: method takes a bool parameter wether the view was displayed with an animation which you are missing. Also you have to call the implementation of the superclass:

- (void)viewDidAppear:(BOOL)animated
{
   [super viewDidAppear: animated];
   NSLog(@"Called viewDidAppear");
}

In your app delegate implementation, there is a method called:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

This method is called each time the app is launched, so I think it fits your needs. If you place your code here, it should work.

Also, be aware you should not perform a synchronous call here, because you will delay the app launch.

EDIT: This method will be only called when the app launches. You could place your code inside a method, and call it from application didFinishLaunchingWithOptions, and then also call it from the method:

- (void)applicationWillEnterForeground:(UIApplication *)application;

This method will be called when the application enters the foreground, but not after the first launch, so beware.

I also think you should check the UIApplicationDelegate methods from apple developer page: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html Also, check out the application state changes: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top