开发者

How to show MBProgressHUD on iPhone without spawning new threads?

开发者 https://www.devze.com 2023-04-08 20:30 出处:网络
I want to show an MBProgressHUD in my iPhone app without spawning new threads. I have a very complicated set of business logic which sometimes (but not always) needs to wait for user input, and runn

I want to show an MBProgressHUD in my iPhone app without spawning new threads.

I have a very complicated set of business logic which sometimes (but not always) needs to wait for user input, and running on multiple threads ends up asking for user input multiple times at once, leading to crazy errors. Thus I would prefer to avoid running anything off of the main thread. However, due to this constraint, MBProgressHUD is not showing because the main thread is being blocked! Normally I would create my MBProgressHUD with the following code:

[HUD showWhileExecuting:@selector(myWorkerMethod开发者_如何转开发) onTarget:self withObject:nil animated:YES];

But I would like to use the following code without blocking the main thread:

HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.minShowTime = 0.0;
HUD.labelText = @"some text";
[HUD show:YES];

Any thoughts?


How about this?

MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = @"Foo";
// code to execute synchronously
[MBProgressHUD hideHUDForView:self.view animated:YES];


Better late than never. You can do this with some run loop tickery. See this answer for details.

0

精彩评论

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