I am trying to do something very simple: update a few labels on my (one and only) view controller when the application becomes active after coming from the background (say with the current time for the simplicity of it (even though in my case, I am updating telephony parameters, such as call states, etc., but that should not matter)).
Honestly, I was sure that once I call viewDidLoad, the code in this method runs again and so all my labels will be up to date. But that doesnt happen, unless I kill the app and relaunch it, so what I did in the app delegate under applicationDidBecomeActive, I called my view controller method:
ViewController *vc = [[ViewController alloc] init];
[vc refreshCalls];
Which does execute this code, but nothing happens to the labels. When doing this, I am also showing an UIAlertView that DOES have the updated info on it, but not the labels.
How can I fix this? Any ideas would be very appreciated.
Here is my app delegate applicationDidBecomeActive method:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
ViewController *vc = [[ViewController alloc] init];
[vc refreshCalls];
}
And the refreshCalls method in the view controller:
-(void) refreshCalls {
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *currentTime = [dateFormatter stringFromDate:today];
NSLog(@"User's current time in their preference format:%@",currentTime);
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"TIME" message:currentTime delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil , nil];
[alert1 show]; --> does show an update time
myLabel.text = currentTime;
[self.view addSubview:myLabel]; --> nothing happens, stays the last time
}
First of all, you shouldn’t call
ViewDidLoad. It gets called for you, just likeapplicationDidBecomeActive.Now, to the answer:
What is happening is that you are calling your
refreshCallsmethod on a newViewControllerinstead of your existing one, which you probably created somewhere in your app delegate. When you first created yourViewController, you probably added its view toUIWindow.Then, when you create a new
ViewControllerinapplicationDidBecomeActiveand callrefreshCallson it, it gets updated just like it should, but its view is not shown on the screen, because you didn’t add it toUIWindow.Try to locate in your
appDelegatewhere do you first create yourViewControllerand assign it to a property. Then, instead of creating a newViewControllerinapplicationDidBecomeActive, get your existing one and call yourrefreshCallsmethod on it.