推动视图控制器两次
在我的应用程序中,我遇到以下错误的问题:
Pushing the same view controller instance more than once is not supported
这是一个来自少数用户的错误报告。 我们试图复制它但不能(双击按钮等)。 这是我们用来打开视图控制器的行:
let storyboard = UIStoryboard(name: "Main", bundle: nil) let editView = storyboard.instantiateViewControllerWithIdentifier("EditViewController") as! EditViewController editView.passedImage = image editView.navigationController?.setNavigationBarHidden(false, animated: false) if !(self.navigationController!.topViewController! is EditViewController) { self.navigationController?.pushViewController(editView, animated: true) }
有人有什么想法吗? 我已经完成了一些研究,并且我们已经涵盖了Stack上的大多数答案,因此对于如何调查而言有点不知所措。
试试这个以避免两次推送同一个VC:
if !(self.navigationController!.viewControllers.contains(editView)){ self.navigationController?.pushViewController(editView, animated:true) }
由于pushViewController从iOS7开始是异步的,如果点击推动视图控制器太快的按钮,它将被推送两次。 我遇到过这样的问题,我尝试的唯一方法是在调用push时设置一个标志(即- navigationController:willShowViewController:animated:
并在调用UINavigationController的委托时取消设置标志- navigationController:didShowViewController:animated:
这很难看,但它可以避免两次推送的问题。
CATransaction
的救援完成块:)
pushViewController(:animated:)
实际上被推送到CATransaction
堆栈,该堆栈由run loop
每次迭代创建。 因此,一旦push
动画完成,就会调用CATransaction
的完成块。
我们使用一个布尔变量isPushing
来确保在推送一个新的视图控制器时无法按下它。
class MyNavigationController: UINavigationController { var isPushing = false override func pushViewController(_ viewController: UIViewController, animated: Bool) { if !isPushing { isPushing = true CATransaction.begin() CATransaction.setCompletionBlock { self.isPushing = false } super.pushViewController(viewController, animated: animated) CATransaction.commit() } } }
- iOS通过UIWebView访问Safari Readerfunction
- ' – :无法识别的select器发送到实例' – 试图找出原因
- CodeSign error:SDK'iOS 8.4'中产品types'WatchKit Extension'需要代码签名
- 在iOS iOS Xamarin上更改背景
- 如何在SplitView控制器中隐藏和取消隐藏Master View控制器
- 如何根据十进制值设置货币格式?
- 在iOS上的Safari上的<select>元素上消除箭头和样式
- 从最新的iTunes 12.7删除应用程序选项…,如何安装adhoc IPA?
- EXC_BAD_ACCESS使用在8.3设备上运行的gmaps sdk 1.9.0,Xcode 6.4