在swift中删除完成处理程序中的UIActivityIndicator
问题描述:
我想在完成块之后删除我的活动指示器。所以我做了这样的事情。在swift中删除完成处理程序中的UIActivityIndicator
func workflowSelected()
{
self.btnWorkflow.setTitle(dm.workflowName, for: .normal)
self.actIndApprover=UIActivityIndicatorView.init(frame: CGRect.init(x: self.btnFirstApprover.frame.size.width-40, y: 5, width: 30, height: 30))
self.actIndApprover.color=self.com.getfontColor()
self.actIndApprover.startAnimating()
self.btnFirstApprover.addSubview(self.actIndApprover)
self.callingFirstApprovers() { (staus) in
self.actIndApprover.stopAnimating()
self.actIndApprover.removeFromSuperview()
self.actIndApplyLeave.stopAnimating()
self.actIndApplyLeave.removeFromSuperview()
self.btnFirstApprover.addTarget(self, action: #selector(self.approverClicked), for: .touchUpInside)
if(staus)
{
print("Approvers loaded")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "approverSelected"), object: nil)
}
else
{
self.btnFirstApprover.setTitle(self.lan.getConvertedLanguageString(word: "First Approver"), for: .normal)
}
}
}
但是这个activityindicator永远不会删除。我是否需要在主线程上删除它?如果是这样,我该怎么做。请帮帮我。 感谢
UPDATE
func callingFirstApprovers(callback:@escaping(Bool)->Void)
{
let strBindedParam="workflowId=\(dm.workflowID!)&requesterCode=\(dm.strEmpCode!)&levelId=1"
let appendString=com.getServicePath(funcNAme: "workflow")
restAPI.prepareUrl(baseUrl:dm.tenantUrl,appendString: appendString, bindedParams: strBindedParam, isAuthorized: true, method: .get, jsonBody: [:]) { (status) in
if(status=="success")
{
self.com.removeProgress()
print(self.dm.responseArray)
self.arrayApproversFullArray=self.dm.responseArray
if(self.dm.responseArray.count>0 && self.dm.responseArray != nil)
{
let obj=self.dm.responseArray[0] as! [String:Any]
print("object approver------\(obj)")
if let objApprover:Array<Any> = obj["Approvers"] as? Array<Any>
{
if(objApprover.count>0)
{
self.dm.arrayFirstApprovers=objApprover
let approverInnerObj=objApprover[0] as! [String:Any]
print(objApprover)
if let strApproverCode=approverInnerObj["EmployeeCode"] as? Int
{
self.dm.strApproverCode=String(strApproverCode)
}
if let strApproverNAme=approverInnerObj["Name"] as? String
{
self.btnFirstApprover.setTitle(strApproverNAme, for: .normal)
self.dm.strApproverNAme=strApproverNAme
}
print("Approvers are more than 1")
callback(true)
}
else
{
callback(false)
}
}
callback(false)
}
else{
callback(false)
}
}
else
{
print(self.dm.errorMessage)
self.com.showAlertMessage()
callback(false)
}
}
}
答
所以是的,GUI的变化必须是使主线程。
DispatchQueue.main.async {
self.actIndApprover.stopAnimating()
}
通常Xcode会在控制台输出中给出警告。
我这样做,似乎它的呼叫多次。有什么理由呢? – user1960169
此调度块正在运行多次 – user1960169
我不知道您的代码或调用。这不是调度本身,这是你的实施多次调用 – Matz