Update text in message

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

UPDATE 3: adding more code! In the .h file, I declare the following (among other things):

@interface WebViewController : UIViewController <UIWebViewDelegate> { IBOutlet UIWebView *webView; UIAlertView *alert; } 

I @property and @synthesize UIAlertview for.

Next, I create a warning in IBAction, which is triggered by clicking a button:

 -(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"]; } 

Then it proceeds 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 ASIHTTPRequest sending data to the web page, and when this request is completed, the code finally goes to the last exit method from UIAlertView and closes everything:

 - (void)requestFinished:(ASIHTTPRequest *)request { [timer invalidate]; [alert dismissWithClickedButtonIndex:0 animated:YES]; backButton.enabled = YES; [alert release]; } 

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

As it is now, the code runs only the very first setMessage -> "get list" and the very last -> "convert list". SetMessage requests in the middle do not work.

Hope someone can help me here!

Thanks everyone!

+4
source share
1 answer

Now I see the problem.

When you update the message property, it does not start redrawing the view immediately. The view is marked as "need to be drawn", and the actual drawing occurs later, usually at the end of the current or next runloop of the main thread.

Therefore, while your didPresentAlertView method didPresentAlertView running in the main thread, the warning view is not redrawn until the method completes. This is why intensive computing work must be performed in a separate thread in order to increase the responsiveness of the user interface, since the job associated with the UI is performed in the main thread.

What you need to do is run your //A lot of code //more code and //even more code in a separate thread and update the message property in the main thread.

For example, your code might look something like this:

  // 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 work with iOS 4.0 and later and want to use GDC, be careful because this can detect the independence of your calculations and message updates and allow them to happen at the same time (which is not advisable here).

+5
source

Source: https://habr.com/ru/post/1341002/


All Articles