如何在显示MBProgressHUD时显示uiviewcontroller

时间:2012-01-14 17:03:17

标签: objective-c nsthread presentmodalviewcontroller mbprogresshud

我正在尝试在单击按钮时显示MBProgressHUD并在作业结束时显示uiviewcontroller

-(IBAction)submitForm:(id)sender{
[self hideKeyboard];
if([self checkUITextField]){
    HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.navigationController.view addSubview:HUD];
    HUD.mode = MBProgressHUDModeIndeterminate;
    HUD.dimBackground = YES;
    HUD.delegate = self;
    HUD.labelText = @"Convalido registrazione";
    [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];
}
}


-(void)myTask{
.
.
.
sleep(3);
[HUD hide:YES];
HUD.customView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]] autorelease];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = @"Richiesta inviata!";
[HUD show:YES];
sleep(1);
UIApplication* app = [UIApplication sharedApplication];
[app performSelectorOnMainThread:@selector(showSingle) withObject:nil waitUntilDone:FALSE];
}

-(void)showSingle{
SingleViewController *single = [[SingleViewController alloc] initWithNibName:@"SingleViewController" bundle:nil];
[self presentModalViewController:single animated:YES];
[self release];
}

我检索到这个错误:

ApplicationName[954:11f03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIApplication showSingle]: unrecognized selector sent to instance 0x7b6d8e0'
*** First throw call stack:
(0x1a53052 0x1be4d0a 0x1a54ced 0x19b9f00 0x19b9ce2 0x1a54e72 0x108f9ef 0x1a2797f 0x198ab73 0x198a454 0x1989db4 0x1989ccb 0x3d7c879 0x3d7c93e 0x777a9b 0x226f 0x21e5)
terminate called throwing an exception

所以我尝试在myTask中直接调用[self showSingle]但在EXC_BAD_ACCESS上生成错误presentModalViewController并在控制台中检索此错误

    [1004:1651b] bool _WebTryThreadLock(bool), 0x7bd6160: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
1   WebThreadLock
2   -[UIWebView _webViewCommonInit:]
3   -[UIWebView initWithCoder:]
4   UINibDecoderDecodeObjectForValue
5   -[UINibDecoder decodeObjectForKey:]
6   -[UIRuntimeConnection initWithCoder:]
7   UINibDecoderDecodeObjectForValue
8   UINibDecoderDecodeObjectForValue
9   -[UINibDecoder decodeObjectForKey:]
10  -[UINib instantiateWithOwner:options:]
11  -[UIViewController _loadViewFromNibNamed:bundle:]
12  -[UIViewController loadView]
13  -[UIViewController view]
14  -[UIViewController viewControllerForRotation]
15  -[UIViewController _visibleView]
16  -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:]
17  -[UIViewController presentViewController:withTransition:completion:]
18  -[UIViewController presentViewController:animated:completion:]
19  -[UIViewController presentModalViewController:animated:]
20  -[FormViewController showSingle]
21  -[FormViewController myTask]
22  -[NSObject performSelector:withObject:]
23  -[MBProgressHUD launchExecution]
24  -[NSThread main]
25  __NSThread__main__
26  _pthread_start
27  thread_start

我该怎么办?你能救我吗?

1 个答案:

答案 0 :(得分:2)

selector需要是接收对象的方法。在这种情况下,showSingle被定义为当前class的实例方法,而不是UIApplication。因此,您应该将邮件发送到performSelectorOnMainThread:,而不是将UIApplication邮件发送到self

[self performSelectorOnMainThread:@selector(showSingle) 
                       withObject:nil 
                    waitUntilDone:FALSE];