0

我試圖表明MBProgressHUD時被點擊,並在作業結束按鈕顯示一個UIViewController如何顯示的UIViewController而MBProgressHUD顯示

-(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 

我該怎麼辦?你可以幫我嗎?

回答

2

selector需要是接收對象的方法。在這種情況下,showSingle被定義爲您當前的class的實例方法,而不是UIApplication。因此,您不應將performSelectorOnMainThread:消息發送至您的UIApplication,而應將消息發送至self

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

我會試試看,謝謝! – JackTurky 2012-01-14 18:18:48

+0

這工作。傑出的示例代碼,謝謝Jonkroll – jcrowson 2012-07-16 23:11:34