我在我的 tableview 上使用 UIRefreshControl
来更新项目。最后,我显示一个 UIAlertController
来通知用户更新已完成,以及更新了多少项目。非常简单,而且在一件事上效果很好。如果我连续多次拉动刷新,有时刷新控件不会被解除,即使在解除警报后也是如此。我需要向上滑动表格才能让它消失。
这是我使用的代码,所有 UI 的东西都在主线程上很好地完成了:
if(refreshControl.refreshing) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self refreshItems];
dispatch_async(dispatch_get_main_queue(), ^{
[refreshControl endRefreshing];
[self.tableView reload];
[self showUpdateInfo];
});
});
}
知道是什么原因造成的吗?
编辑:这就是我在代码中创建刷新控件的方式(在 viewDidLoad
中):
UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString: @"Checking for updates…"];
[refreshControl addTarget: self
action: @selector(refreshOutdatedItems)
forControlEvents: UIControlEventValueChanged];
self.refreshControl = refreshControl;
最佳答案
延迟任意时间并不是一个非常干净的解决方案。在我看来,通过加倍到 1000 万来实现延迟也有点不合时宜。
我最终做的与这个伪 Swift 类似:
let alert = UIAlertController(...)
present(alert, animated: true) {
refreshControl.endRefreshing()
}
它会延迟对 endRefreshing() 的调用,直到出现警报为止。
我使用 Swift 3 和 PromiseKit 的实际代码看起来更像这样:
@IBAction func onRefresh() {
Repository.getNewData().then { data in
self.data = data
self.tableView.reloadData()
}.recover { error in
// Return a promise to make sure endRefreshing isn't called until the alert is presented.
return self.presentErrorAlert(message: "Some user-friendly message")
}.always {
self.refreshControl?.endRefreshing()
}
}
private func presentErrorAlert(message: String) -> Promise<Void> {
return Promise<Void> { fulfill, _ in
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true) {
fulfill()
}
}
}
关于ios - UIAlertController 有时会阻止 UIRefreshControl 隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41534375/