Ios 按两次视图控制器
在我的应用程序中,我遇到以下错误:Ios 按两次视图控制器,ios,swift,uiviewcontroller,uinavigationcontroller,Ios,Swift,Uiviewcontroller,Uinavigationcontroller,在我的应用程序中,我遇到以下错误: Pushing the same view controller instance more than once is not supported 这是一个bug报告,由一些用户重新发布。我们试图复制它,但不能(双击按钮等)。这是我们用来打开视图控制器的行: let storyboard = UIStoryboard(name: "Main", bundle: nil) let editView = storyboard.instantiateViewCon
Pushing the same view controller instance more than once is not supported
这是一个bug报告,由一些用户重新发布。我们试图复制它,但不能(双击按钮等)。这是我们用来打开视图控制器的行:
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)
}
有人有什么想法吗?我做了一些研究,我们讨论过的堆栈上的大多数答案都不知道如何进行调查。由于pushViewController自iOS7以来是异步的,如果您点击按钮将视图控制器推得太快,它将被推两次。 我遇到过这样的问题,我尝试的唯一方法是在调用push时设置标志(即
-navigationController:willShowViewController:animated:
),并在调用UINavigationController的委托时取消设置标志-navigationController:didShowViewController:animated:
这很难看,但它可以避免两次推送问题。尝试以下方法以避免将同一个VC推送两次:
if !(self.navigationController!.viewControllers.contains(editView)){
self.navigationController?.pushViewController(editView, animated:true)
}
CATransaction
到救援的完成块:)
pushViewController(:animated:)
的动画实际上被推送到CATransaction
堆栈上,该堆栈由运行循环的每次迭代创建。因此,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()
}
}
}
在执行推送的功能中:
guard navigationController?.topViewController==self-else{return}
您如何推荐Dershowitz123?如果需要,请尝试!(self.navigationController!.topViewController!是EditViewController.self)
。苹果为什么要让它异步?这是一个非常好的解决方案,但并不能100%工作。例如,当segue正在进行时。很好,但它并不总是阻塞。它在我的应用程序中的所有位置都有效,除了一个:我有PageViewController,从它里面的控制器我调用pushViewController。它正在调用重写的函数,但有时在第一次推送完成后第二次调用。我不得不在这门课上再加一道封锁线。