2

I'm having a problem with the enabling/disabling of a UIButton, disabling the button works fine if I don't enable it again later in my code. When I do enable it later on it shows up as though it's disabled (the opacity changes) yet when I press it the attached IBAction function is still called.

The code:

- (void)loadDataFromURL:(NSURL *)URL withLoadIndicator:(UIActivityIndicatorView *)loadIndicator errorName:(NSString *)name sender:(id)sender andCallback:(SEL)selector{

    // Start loading indicator, block button so we will have only one call at a time
    [loadIndicator startAnimating];

    if ([sender isKindOfClass:[UIButton class]]) {
        UIButton *button = sender;
        [button setEnabled:NO];
    }

    // Run the data load sequence
    self.dispatchQueue = dispatch_queue_create("com.companyname.settingsqueue", 0);

    dispatch_async(self.dispatchQueue, ^{
        // Downloading JSON and using CoreData to put it into the sqlite database here
    });

    // After loading is complete stop animating and re-enable the button
    dispatch_async(dispatchQueue, ^{
        [loadIndicator stopAnimating];
        if ([sender isKindOfClass:[UIButton class]]) {
            UIButton *button = sender;
            button.enabled = YES;
        }
    });
}

The strange thing is it works perfectly for the loadIndicator. The button I get from the sender parameter in the function does exist (it's not null). When I remove button.enabled = YES; it stays disabled as it should. Is there a way to enable it again after my async code has executed without the button still being enabled during the async execution?

Thanks in advance for helping me.

4

1 回答 1

2

I found the answer to my problem after extensive searching. Because I wasn't running all my UIView functions on the main thread the UI didn't update even though my function finished. Therefor the button disabled for a really short time and then enabled again, even though my overlaying UIView and UIActivityIndicator were not yet removed from the screen.

I hope this helps someone else with similar issues.

于 2012-09-30T15:03:44.297 回答