2011-01-10 126 views
0

我的UIAlertViews有一个小问题。我试图展示它,执行一些任务,然后自动解除。这是我目前使用的代码:UIAlertView不显示

callingTaxi = [[UIAlertView alloc] initWithTitle:@"" message:@"検索中" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil]; 
[callingTaxi show]; 
/* Do some tasks */ 
[callingTaxi dismissWithClickedButtonIndex:0 animated:YES]; 
[callingTaxi release]; 

然而,UIAlertView中只显示一半。我可以看到背景变暗,但是在任务完成后,警报视图迅速出现,然后再次消失。

任何想法如何解决这个问题?

+0

能否详细说明您的解决方案,因为我有同样的问题,谢谢! – Rasman 2011-05-29 20:03:55

回答

3

它做节目,但你关闭它马上,而不是等待用户做一些事情,与

[callingTaxi dismissWithClickedButtonIndex:0 animated:YES]; 

没有时间为iOS完全呈现它。无论如何,这不是应该如何使用dismissWithClickedButtonIndex。你想在这里做什么?

编辑:我想你需要分配一个委托给UIAlertView,并让委托处理AlertView内发生的事情。

+0

谢谢。解决我的问题:) – Ben 2011-01-10 14:05:14

1

你不应该关闭这表明它同样的功能里面,由计时器关闭它,或作为反应到另一个事件。

0

你应该为alertview

- (void) alertView: (UIAlertView *) alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
// Do stuff 
// Or if you have multiple buttons, you could use a switch 

[callingTaxi release]; 

或者你可以自动释放创建的方法..

但x3ro给了正确的答案,你自己调用该方法,而不是等待用户按按钮..

1

你不需要委托。问题在于你所做的任务必须发生在后台线程上,所以主线程会独自更新屏幕。

如果您更新代码以使用块和调度队列,这样就可以了:

callingTaxi = [[UIAlertView alloc] initWithTitle:@"" message:@"検索中" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil]; 
[callingTaxi show]; 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, NULL), ^{ 
    /* Do some tasks */ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     // this code is back on the main thread, where it's safe to mess with the GUI 
     [callingTaxi dismissWithClickedButtonIndex:0 animated:YES]; 
     [callingTaxi release]; 
    }); 
}); 
0

的UIAlertView中被驳回: [callingTaxi dismissWithClickedButtonIndex:0动画:是]; 用户可以阅读它之前。

你打算让最终用户阅读它多久?