问题描述
我有一个产品表,每个产品都有一个下载按钮。每个按钮在一个单独的类中都是它自己的 NSTableCellView。如果下载正在进行,我想禁用/隐藏原始产品视图控制器类中的按钮。但是每当我尝试这样做时,我的应用程序都会崩溃,并且几乎没有关于原因的错误消息。有没有办法实现我的目标?
let viewCon = ProductViewController()
func urlSession(_ session: URLSession,downloadTask: URLSessionDownloadTask,didWriteData bytesWritten: Int64,totalBytesWritten: Int64,totalBytesExpectedToWrite: Int64) {
let filesize: Int64 = Int64(passedLongBytes)!
let percentage = CGFloat(totalBytesWritten) / CGFloat(filesize)
dispatchQueue.main.async{
self.shapeLayer.strokeEnd = percentage
print("PERCENTAGE: \(Int(percentage * 100))%")
if((Int(percentage * 100) < 100)){
self.viewCon.backButtonOutlet.isHidden = true
}else{
self.viewCon.backButtonOutlet.isHidden = false
}
}
}
我只是收到一条错误消息“线程 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP,subcode=0x0)”
解决方法
所以我想出了一个替代解决方案,我创建了一个全局 Int 变量来检查百分比,然后我可以在我的其他 ProductViewController 中使用该全局变量。我不知道这是否是最好的方法,但它似乎达到了我的目标。
CellView
func urlSession(_ session: URLSession,downloadTask: URLSessionDownloadTask,didWriteData bytesWritten: Int64,totalBytesWritten: Int64,totalBytesExpectedToWrite: Int64) {
let filesize: Int64 = Int64(passedLongBytes)!
let percentage = CGFloat(totalBytesWritten) / CGFloat(filesize)
DispatchQueue.main.async{
self.shapeLayer.strokeEnd = percentage
globalPercentage = (Int(percentage * 100))
}
}
ProductviewController
@IBAction func backButtonClicked(_ sender: NSButton) {
addFileInstall.removeAll()
addFileUpdates.removeAll()
print(globalPercentage)
if(globalPercentage < 100){
dialogOKCancel(text: "Wait for download to finish before closing this view")
}else{
self.view.window?.close()
}
}
func dialogOKCancel(text: String) -> Bool {
let alert = NSAlert()
alert.informativeText = text
alert.alertStyle = .warning
alert.addButton(withTitle: "OK")
return alert.runModal() == .alertFirstButtonReturn
}