2014-04-28 91 views
0

我有一個非常簡單的方法。然而,在UIAlertView中不會運行的方法....這裏是我的方法:方法沒有運行 - iOS

-(void)post_result { 
    NSLog(@"Post Result"); 

    post_now.enabled = YES; 
    [active stopAnimating]; 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You're post has been successfully uploaded." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
    [alertView show]; 

    success_post_facebook = 0; 
    success_post_youtube = 0; 
    success_post_googleplus = 0; 
    success_post_tumblr = 0; 
    success_post_twitter = 0; 

    NSLog(@"Post Result END"); 
} 

奇怪的是,該代碼之前和之後的UIAlertView中會在此方法運行....那麼地球是錯的??

謝謝你的時間。

+2

調試器中的任何東西?你是否在一個單獨的線程中調用這個「post_result」?如果您希望得到一個好的答案,我會發布更多的代碼。 –

+0

你是什麼意思,它不會運行 - 它不顯示? – thegrinner

+4

確保您在主線程中顯示警報。 – rmaddy

回答

4

你很可能在主線程中調用的不是UIAlertView。爲了讓它在主線程上運行,就像這樣使用主調度隊列。

dispatch_async(dispatch_get_main_queue(), ^{ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You're post has been successfully uploaded." delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
    [alertView show]; 
}); 
+0

不應該是'dispatch_sync()'? – trojanfoe

+0

謝謝,你也可以做[[NSOperationQueue mainQueue] addOperationWithBlock:我想。問題在於我通過內置的Twitter框架從Twitter API調用中調用該方法。我認爲這是讓它在一個單獨的線程上運行。 – Supertecnoboff

+0

@trojanfoe當分派的代碼在主隊列上運行時,它只需要'dispatch_sync'就是後臺線程被阻塞。在這種情況下沒有意義。 – rmaddy