简体   繁体   English

UIAlertView不解雇?

[英]UIAlertView not dismissing?

I have a few ViewController subclasses inside a UINavigation controller. 我在UINavigation控制器中有一些ViewController子类。 I have successfully used UIAlertViews elsewhere in the application, and I know how to set the delegate and include the correct delegate methods, etc. 我已经成功地在应用程序的其他地方使用了UIAlertViews,我知道如何设置委托并包含正确的委托方法等。

In a ViewController with a UITableView, I have implemented a 'pull to refresh' with a UIRefreshControl. 在带有UITableView的ViewController中,我使用UIRefreshControl实现了“pull to refresh”。 I have a separate class to manage the downloading and parsing of some XML data, and in the event of a connection error, I post a notification. 我有一个单独的类来管理一些XML数据的下载和解析,如果出现连接错误,我会发布一个通知。 The view controller containing the table view observes this notification and runs a method where I build and display an alert: 包含表视图的视图控制器会观察此通知并运行我构建和显示警报的方法:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Connection Error" message:[[notification userInfo] objectForKey:@"error"] delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil];
alertView.alertViewStyle = UIAlertViewStyleDefault;
[alertView show]; 

The alert displays correctly, but the cancelButton is unresponsive - there is no way to dismiss the alert! 警报显示正确,但cancelButton没有响应 - 没有办法解除警报! Putting similar code (identical, but without the notification's userinfo) in the VC's viewDidLoad method creates an alert that behaves normally. 在VC的viewDidLoad方法中放置类似的代码(相同但没有通知的userinfo)会创建一个行为正常的警报。

Is the refresh gesture hogging first responder or something? 刷新手势是否会占用第一响应者或其他什么? I have tried [alertView becomeFirstResponder]. 我试过[alertView becomeFirstResponder]。 I would be grateful for any advice… 我会很感激任何建议......

Update: screenshot included… is this the right info? 更新:包含截图...这是正确的信息吗? (can't embed this image for lack of reputation) http://i.stack.imgur.com/4CGqS.png (因缺乏声誉而无法嵌入此图片) http://i.stack.imgur.com/4CGqS.png

Edit 编辑

It seems like you have a deadlock or your thread is stuck waiting. 看起来你有一个死锁或你的线程等待。 You should look at your code and see what causes this. 您应该查看代码并查看导致此问题的原因。

Original answer which lead to update in OP 在OP中导致更新的原始答案

Make sure the alert is shown on the main thread: 确保主线程上显示警报:

dispatch_async(dispatch_get_main_queue(), ^{
    //Open alert here
});

This isn't a solution per se, but I might try two quick things as you troubleshoot: 这本身不是一个解决方案,但我可能会在您排除故障时尝试两个快速的事情:

1) Hardcode some text in the UIAlert, rather than passing in the notification object. 1)硬编码UIAlert中的一些文本,而不是传入通知对象。 See if there is any change in behavior. 查看行为是否有任何变化。

2) Try adding another button to the alert and an accompanying method to catch it. 2)尝试在警报中添加另一个按钮,并附带一个方法来捕获它。 So you'll see if the delegate is getting ny buttons messages at all. 因此,您将看到委托是否正在获取任何按钮消息。

Try adding a tag to the alertView 尝试在alertView添加标记

alertView.tag = 0;

Then create the method alertView:clickedButtonAtIndex: in the view controller. 然后在视图控制器中创建方法alertView:clickedButtonAtIndex:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    if (alertView.tag == 0) {
        [alertView dismissWithClickedButtonIndex:0 animated:YES];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM