我可以为我重现该错误:如果对我实际上已经关闭的View中的其中一个View进行了更改(popViewControllerAnimated),则会收到错误。
我正在执行一个异步任务(POSTRequest)的UINavigationButton动作。在执行此请求之前,我将该按钮的customView设置为进度指示器。从异步调用返回时,我调度到主线程并重置该按钮的customView。之后,我将关闭视图(popViewControllerAnimated)。
这是没有出现错误的正确代码:
/* right way, working without BSMachError */
@IBAction func sendRequest(sender: UIBarButtonItem) {
/* setting the progress indicator as customView of the self.saveButton */
self.showActivityIndicatory(self.saveButton)
/* asynchronous REST call */
UserDataManager.sharedInstance.requestFeedback(request, onCompletion: { error in
dispatch_async(dispatch_get_main_queue(),{
/* resetting the saveButton again to default by setting customView to nil */
self.saveButton.customView = nil
/* closing the view */
self.navigationController!.popViewControllerAnimated(true)
})
})
}
导致错误的原因是切换了行:关闭视图,然后将customView设置为nil:
/* WRONG way, causing BSMachError */
@IBAction func sendRequest(sender: UIBarButtonItem) {
/* setting the progress indicator as customView of the self.saveButton */
self.showActivityIndicatory(self.saveButton)
/* asynchronous REST call */
UserDataManager.sharedInstance.requestFeedback(request, onCompletion: { error in
dispatch_async(dispatch_get_main_queue(),{
/* closing the view */
self.navigationController!.popViewControllerAnimated(true)
/* resetting the saveButton again to default by setting customView to nil */
self.saveButton.customView = nil
})
})
}