Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate UILabel from applicationDidBecomeActive?
    text
    copied!<p>I want to update UILabel by clicking a reload button. Additionally, I want to update the label in background, because it is fetching the new data via XML from my website. Of course it would be nice to auto-update the label when the application is opened. And there is my problem:</p> <p>I was able to make it work well when user were clicking the button manually. But I don't understand how to do the same by calling my method via "applicationDidBecomeActive". I tried to do it the same way, but it obviously doesn't work because my label is returned nil.</p> <p>I suppose there is a problem of my understanding and the solution should be quite easy. Thanks for your input! Note: I am a beginner with Objective-C and have sometimes problems with "easy" things. ;-)</p> <p>Below is a summary of the important code parts:</p> <p><strong>AppDelegate</strong></p> <pre><code>- (void)applicationDidBecomeActive:(UIApplication *)application { [[MyViewController alloc] reloadButtonAction]; } </code></pre> <p><strong>MyViewController</strong></p> <pre><code>@synthesize label - (void)reloadButtonAction { [self performSelectorInBackground:@selector(updateData) withObject:nil]; } - (void)updateData { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // Parse the XML File and save the data via NSUserDefaults [[XMLParser alloc] parseXMLFileAtURL]; // Update the labels [self performSelectorOnMainThread:@selector(updateLabels) withObject:nil waitUntilDone:NO]; [pool release]; } - (void)updateLabels { NSUserDefaults *variable = [NSUserDefaults standardUserDefaults]; myLabel.text = [variable stringForKey:@"myLabelText"]; // myLabel is nil when calling all of this via AppDelegate // so no changes to the myLabel are done in that case // but: it works perfectly when called via button selector (see below) NSLog(@"%@",myLabel.text); } -(void)viewDidLoad { // Reload button in the center UIButton *reloadButton = [UIButton buttonWithType:UIBarButtonSystemItemRefresh]; reloadButton.frame = CGRectMake(145,75,30,30); [reloadButton setTitle:@"" forState:UIControlStateNormal]; [reloadButton addTarget:self action:@selector(reloadButtonAction) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:reloadButton]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload