开发者

Update text in alert message

Still trying to update the message in an UIAlertview while the alert is active. I removed most of the first part of the question and publish more code in the update below.

UPDATE 3: Adding more code! In the .h file I declare the following (among others):

@interface WebViewController : UIViewController <UIWebViewDelegate> {

IBOutlet UI开发者_Python百科WebView *webView;
UIAlertView *alert;

}

I @property and @synthesize the UIAlertview to.

Next I create the alert in an IBAction which is run by a button click:

-(IBAction)convert {

convertButton.enabled = NO;
mailButton.enabled = NO;
backButton.enabled = NO;

//show the alert window
alert = [[UIAlertView alloc] initWithTitle:@"Converting in progress\nPlease Wait..." message:@"\n\n\n" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[alert show];

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

// Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];

[indicator release];

[alert setMessage:@"getting roster"];
}

It then jumps to the following function:

- (void)didPresentAlertView:(UIAlertView *)progressAlert {

   //A lot of code
      [alert setMessage:@"Checking history"];
   //more code
      [alert setMessage:@"writing history"];
   //even more code
      [alert setMessage:@"converting roster"];
}

The didPresentAlertView method ends with an ASIHTTPRequest to post data to a webpage and when this request is finished the code finally jumps to the last method to exit the UIAlertView and closes everything up:

- (void)requestFinished:(ASIHTTPRequest *)request {

   [timer invalidate];
   [alert dismissWithClickedButtonIndex:0 animated:YES];
   backButton.enabled = YES;
   [alert release];
}

I also removed the autorelease from my UIAlertView init to make sure it exists during the rest of the process.

As it is now, the code only fires the very first setMessage -> 'getting roster' and the very last -> 'converting roster'. The setMessage requests in the middle do not get fired..

Really hope someone can help me here!

Thanks all!


Now I see the problem.

When you update the message property, it doesn't fire redrawing the view right away. The view is marked as 'needed to be drawn', and the actual drawing happens later, typically at the end of the current or next runloop in the main thread.

Therefore, while your didPresentAlertView method is running on the main thread, the alert view is not redrawn until the method is finished. This is why a computation-intensive job needs to run on a separate thread to increase the responsiveness of the UI, as the UI-related job is done on the main thread.

What you should do is run your //A lot of code //more code and //even more code on a separate thread, and update the message property on the main thread.

For example, your code may look similar to :

    // this is inside didPresentAlertView method
    NSOperationQueue* aQueue = [[NSOperationQueue alloc] init];
    [aQueue addOperationWithBlock: ^{
        // A lot of code
        [alert performSelector:@selector(setMessage:) onThread:[NSThread mainThread]
                       withObject:@"Checking history" waitUntilDone:NO];
        // more code
        [alert performSelector:@selector(setMessage:) onThread:[NSThread mainThread]
                       withObject:@"writing history" waitUntilDone:NO];
        // it goes on
    }];

If you are working with iOS 4.0 and later, and want to use the GDC, be careful because it may detect independency of your computation and message updates and let them happen concurrently (which is not desired here).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜