A"尝试过,真实"我在iOS Swift应用程序中使用的模式是我有几个UIButtons区域。按下按钮时,它会触发连接到我的API的一些网络代码。当这种情况发生时,我按下按钮文字说"请等待,加载"我禁用了按钮。当排队的操作在我的回调中完成时,我启用了按钮文本并更改回原始状态。它很棒。
我最近添加了一些不使用NSURL / NSDATA的东西(它只是一个文件编写器)。我复制了所有相同的GCD队列代码,奇怪的是它没有更新按钮文本。
这是我的代码。当您点击按钮时,文本将变为不可见,直到回调结束,然后它才会恢复。奇怪的是,如果我将我的模拟器更改为iPad Pro,它实际上可以工作(??)并说'#34;请等待,下载"。如果我切换到iPhone 6s,它就无法工作。
@IBAction func btnGenerateCSV(sender: UIButton) {
//Grab the original text of the button to restore later after done
let originalButtonText = sender.titleForState(UIControlState.Normal)
//Localized is an extension function I wrote.
//As you can see I got crazy here adding all the UI States as a last ditch attempt to see if that was the reason.
sender.setTitle(Localized("Downloading"), forState: UIControlState.Normal)
sender.setTitle(Localized("Downloading"), forState: UIControlState.Disabled)
sender.setTitle(Localized("Downloading"), forState: UIControlState.Highlighted)
//I've tried moving this before the setTitle. No avail.
sender.enabled = false
//I've tried the other queues as well, and even just tried dispatch_async(dispatch_get_main_queue()) but no luck
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) { [unowned self] in
CsvReportWriter.GenerateReport()
{
r in
dispatch_async(dispatch_get_main_queue()) {
FileManager.WriteToFile(r, filename: self.filename)
if FileManager.FileExists(self.filename) {
self.docController = UIDocumentInteractionController(URL: FileManager.GetURLOfFile(self.filename))
self.docController.presentOptionsMenuFromRect(sender.frame, inView:self.view, animated:true)
}
//Restore button state and text b/c we're done
sender.enabled = true
sender.setTitle(originalButtonText, forState: UIControlState.Normal)
}
}
}
}
有什么想法吗?如果我将CsvReportWriter.GenerateReport()代码替换为调用我的API的其他异步代码,它可以工作。
非常感谢!
答案 0 :(得分:1)
所以我想发布一个我认为很有趣的答案 - 我有一个不同的帐户登录到我的iPad Pro模拟器..并注意到标签正确出现。在iPhone 6中它没有。我登录了另一个帐户。
那有什么区别?在iPad专业版中有大约1,000,000个测试行,在iPhone中有3个,所以一切都完成得很快,甚至没有用户界面甚至更新。
我引入了一个延迟作为测试,并注意到它实际上是工作/导出,所以实际上它是什么。
事实证明事情运行得如此之快,甚至没有时间更新为“请等待”大声笑
很抱歉打扰任何人,但可以随意使用上面的代码,因为它可以有效地满足你的需要:)