Swift OS X禁用/隐藏视图控制器中的按钮(如果正在从单独类中的表视图进行下载)
我有一个产品表,每个产品都有一个下载按钮。每个按钮在单独的类中都有自己的NSTableCellView。如果正在下载,我想禁用/隐藏原始产品视图控制器类中的按钮。但每当我尝试这样做时,我的应用程序就会崩溃,几乎没有关于原因的错误消息。有什么方法可以实现我的目标吗Swift OS X禁用/隐藏视图控制器中的按钮(如果正在从单独类中的表视图进行下载),swift,swift5,nsbutton,Swift,Swift5,Nsbutton,我有一个产品表,每个产品都有一个下载按钮。每个按钮在单独的类中都有自己的NSTableCellView。如果正在下载,我想禁用/隐藏原始产品视图控制器类中的按钮。但每当我尝试这样做时,我的应用程序就会崩溃,几乎没有关于原因的错误消息。有什么方法可以实现我的目标吗 let viewCon = ProductViewController() func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, di
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
}
}
}
让viewCon=ProductViewController()
func urlSession(session:urlSession,downloadTask:URLSessionDownloadTask,didWriteData BytesWrite:Int64,TotalBytesWrite:Int64,totalBytesExpectedToWrite:Int64){
让文件大小:Int64=Int64(passedLongBytes)!
让百分比=CGFloat(totalBytesWrited)/CGFloat(filesize)
DispatchQueue.main.async{
self.shapeLayer.strokeEnd=百分比
打印(“百分比:\(整数(百分比*100))%”)
如果((整数(百分比*100)<100)){
self.viewCon.backButtonOutlet.ishiden=true
}否则{
self.viewCon.backButtonOutlet.ishiden=false
}
}
}
我刚刚收到一条错误消息“Thread 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
}
@IBAction func backButtonClicked(uu发送方:NSButton){
addFileInstall.removeAll()
addFileUpdates.removeAll()
印刷品(全球百分比)
if(全球百分比<100){
dialogOKCancel(文本:“在关闭此视图之前等待下载完成”)
}否则{
self.view.window?.close()
}
}
func对话框OK取消(文本:字符串)->Bool{
let alert=NSAlert()
alert.informativeText=文本
alert.alertStyle=.warning
alert.addButton(标题为“OK”)
返回alert.runModal()==.alertfirstbutton返回
}
创建对backButtonOutlet的弱引用。在您发布的代码中,哪个对象是self
?viewCon
与NSTableCellView
有何关联?是viewCon
还是backButtonOutlet
nil
?我已经在另一个ProductViewController@IBOutlet弱var backButtonOutlet:NSButton中对backButtonOutlet有一个弱引用!我发现如果我将其设置为可选的self.viewCon.backButtonOutlet?.ishiden=true,则应用程序不会崩溃,但按钮不会隐藏。