2014-04-28 92 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