Reputation: 3
I need help with updating a label through in applicationDidBecomeActive
. The way my project is structured, I have a RootViewController. When the user touches a button, a new view controller is opened through presentModalView
. I want to update the label on this view controller each time the application becomes active. This is my code in my app delegate:
WordList1 *viewController1 = [[WordList1 alloc] initWithNibName:@"WordList1" bundle:nil];
[viewController1 changelabel:counter];
[viewController1 release];
And it calls this function in my WordList1 viewcontroller to update the label each time:
- (void) changelabel:(int)counter {
NSString *string = [NSString stringWithFormat:@"%d", counter];
label.text = string;
}
When I run it through debugger, the value of "counter" and "string" are updating, but the label is not changing values. Please help
Upvotes: 0
Views: 1057
Reputation: 1790
Where have you created your UILabel
? If it is created through your xib file, make sure you have connected the File Owner's IBOutlet
to the correct label. If not connected, it does not know what label to update.
EDIT: I believe that updating your label from the AppDelegate is bad code design. The label should really be updated from a view controller. My guess is that you are putting the code in the AppDelegate because that's where the code is that is called when the app enters the foreground. To update the label from a view controller when the app enters the foreground, you can set up a notification in the view controller. Code example:
- (id)init
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeLabel:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
return self;
}
This registers the view controller as an observer to be notified each time the app will enter the foreground. This way, you can contain this code in your view controller, and have it be updated accordingly.
Also, make sure you unregister the observer when the view controller is deallocated, by adding the following to your dealloc method:
[[NSNotificationCenter defaultCenter] removeObserver:self];
Upvotes: 3
Reputation: 5953
In your appdelegate code, you never use ViewController1. It's not set as rootViewController, nor pushed nor nothing? I don't understand how it can display anything?
Upvotes: 0