开发者

Why aren't my UILabels refreshing with new information after an NSNotificationCenter update?

开发者 https://www.devze.com 2023-01-24 21:16 出处:网络
I am working on a weather app, and have everything working perfectly...Except the UILabels that are displaying the data. Once the app loads for the first time, it goes through and finds the data corre

I am working on a weather app, and have everything working perfectly...Except the UILabels that are displaying the data. Once the app loads for the first time, it goes through and finds the data correctly and then displays it.

This is what one of the UILabels looks like, inside my main RootViewController:

UILabel *myCityLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 200, 80)];
[myCityLabel setText:[NSString stringWithFormat:@"%@",placemark.locality]];
myCityLabel.textAlignment = UITextAlignmentLeft;
myCityLabel.textColor = [UIColor blackColor];
myCityLabel.font = [UIFont fontWithName:@"Helvetica" size:24];
myCityLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:myCityLabel];
[myCityLabel release];

I have CoreLocation running in the background. Inside my appDelegate, I see these two methods get called once the app is invoked again (after closing it):

- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"applicationDidBecomeActive:");

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

/*
 Restart any tasks that were paused (or not yet started) while the application was inactive. If the application w开发者_运维技巧as previously in the background, optionally refresh the user interface.
 */

lbsViewController = [[LBSViewController alloc] init];
[lbsViewController viewDidLoad];
}

- (void)deviceNotificationReceived:(NSNotification *)notification
{
    NSLog(@"was this received?");
}

What I am trying to do here is to launch the viewDidLoad method of lbsViewController (my main RootViewController. I can see via the console that it is returning new information, and even that the function viewDidLoad is being called, but the labels aren't refreshing with the new data...any suggestions on what route(s) I can take to fix this problem?

I should note that the only time the UILabels are refreshing with new data is upon building the app from Xcode to my device.


You say that you solved the problem by removing multitasking. Your problem was that the NSNotification you sent from the background thread arrived on the same background thread, and you tried to update your UILabel from that background thread. That isn't allowed - you must update UI elements from the main thread.

To solve this, you can marshall the call to the main thread using:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait

so, something like this (in your notification handler):

[viewController.outputLabel performSelectorOnMainThread: @selector( setText: ) withObject: currentChar waitUntilDone: YES];

note, this is the same response I gave to this question: How do I display characters to a UILabel as I loop through them?


The two lines:

lbsViewController = [[LBSViewController alloc] init];
[lbsViewController viewDidLoad];

Are allocating a brand new LBSViewController and calling viewDidLoad on it. Probably you want to call viewDidLoad on your existing lbsViewController object rather than allocating a new one (ie, remove the first of those two lines).

In any case, it'd be better to have the view controller observe the notification itself and deal with everything internally. This sort of overloads the meaning of viewDidLoad, and adding a method with an alternative name arguably distributes the logic in an odd way.

0

精彩评论

暂无评论...
验证码 换一张
取 消