开发者

Update UILabel from applicationDidBecomeActive?

开发者 https://www.devze.com 2023-02-11 02:13 出处:网络
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

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:

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 开发者_StackOverflowobviously doesn't work because my label is returned nil.

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. ;-)

Below is a summary of the important code parts:

AppDelegate

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [[MyViewController alloc] reloadButtonAction];
 }

MyViewController

@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];
}


First:

[[MyViewController alloc] reloadButtonAction];

Doesn't make sense. You allocate memory, without initializing an object. And then you want to call a method on it. Doesn't work Use an instance for it:

[myViewControllerInstance reloadButtonAction];

In your app delegate you should have an reference to your rootcontroller instance if that is the object contains the reload method, use that instance.

Note: Alloc only reserves space in the memory for an object which size the size of MyViewController instance. An init method will fill it.

0

精彩评论

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

关注公众号