UINavigation 将同一视图控制器推送两次错误

Posted

技术标签:

【中文标题】UINavigation 将同一视图控制器推送两次错误【英文标题】:UINavigation is pushing the same view controller twice error 【发布时间】:2020-08-13 22:54:55 【问题描述】:

将视图控制器推送到堆栈时出现以下错误:

Thread 1: Exception: "<UINavigationController: 0x7f9345817a00> is pushing the same view controller instance (<xxxx.HelpViewController: 0x7f9346143390>) more than once which is not supported and is most likely an error in the application : com.xxx.xxx1"

我使用以下代码从表视图控制器调用视图控制器:

let myVC = self.storyboard?.instantiateViewController(identifier: "HelpViewController") as! HelpViewController
                // pass any parms here
                myVC.stringPassed = label!.text!
                navigationController?.pushViewController(myVC, animated: true)
                self.navigationController!.pushViewController(myVC, animated: false)

我只有一个视图控制器副本,这是我第一次将此视图推送到堆栈上。

任何帮助将不胜感激。

这是日志:

2020-08-13 17:42:45.289742-0500 NCLEX[29470:2944371] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '<UINavigationController: 0x7f94e980ce00> is pushing the same view controller instance (<xxx.HelpViewController: 0x7f94e54634a0>) more than once which is not supported and is most likely an error in the application : com.xxx.xxx'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23e3de6e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff512539b2 objc_exception_throw + 48
    2   UIKitCore                           0x00007fff48be2df6 -[UINavigationController pushViewController:transition:forceImmediate:] + 451
    3   UIKitCore                           0x00007fff48be2a9f -[UINavigationController pushViewController:animated:] + 669
    4   UIKitCore                           0x00007fff48ca880d -[_UIViewControllerTransitionCoordinator _applyBlocks:releaseBlocks:] + 294
    5   UIKitCore                           0x00007fff48ca495b -[_UIViewControllerTransitionContext _runAlongsideCompletions] + 126
    6   UIKitCore                           0x00007fff48ca46cf -[_UIViewControllerTransitionContext completeTransition:] + 106
    7   UIKitCore                           0x00007fff48cb6e1f __53-[_UINavigationParallaxTransition animateTransition:]_block_invoke.158 + 806
    8   UIKitCore                           0x00007fff4982093c -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 671
    9   UIKitCore                           0x00007fff497f15c9 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 220
    10  UIKitCore                           0x00007fff497f1bc0 -[UIViewAnimationState animationDidStop:finished:] + 276
    11  UIKitCore                           0x00007fff497f1d46 -[UIViewAnimationState animationDidStop:finished:] + 666
    12  QuartzCore                          0x00007fff2b4fbadc _ZN2CA5Layer23run_animation_callbacksEPv + 306
    13  libdispatch.dylib                   0x0000000110475e8e _dispatch_client_callout + 8
    14  libdispatch.dylib                   0x0000000110483d97 _dispatch_main_queue_callback_4CF + 1149
    15  CoreFoundation                      0x00007fff23da1869 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    16  CoreFoundation                      0x00007fff23d9c3b9 __CFRunLoopRun + 2041
    17  CoreFoundation                      0x00007fff23d9b8a4 CFRunLoopRunSpecific + 404
    18  GraphicsServices                    0x00007fff38c39bbe GSEventRunModal + 139
    19  UIKitCore                           0x00007fff49325968 UIApplicationMain + 1605
    20  NCLEX                               0x000000010f37007b main + 75
    21  libdyld.dylib                       0x00007fff520ce1fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

【问题讨论】:

【参考方案1】:

如果您不需要动画,则需要注释这 2 行中的 1 行,主要是第二行 1

navigationController?.pushViewController(myVC, animated: true)
self.navigationController!.pushViewController(myVC, animated: false)

因为他们做同样的事情是两次推动相同的 vc 导致崩溃

【讨论】:

天哪,我怎么会错过这个!谢谢我的朋友,很抱歉这个愚蠢的问题.....我非常尴尬。

以上是关于UINavigation 将同一视图控制器推送两次错误的主要内容,如果未能解决你的问题,请参考以下文章

如何防止用户从滑出设置菜单中连续两次调用同一个视图控制器?

推送视图控制器不起作用

推送视图控制器后从情节提要加载视图

如何停止将 UIViewController 推到自身之上?

设置 UINavigation 控制器和各种视图加载

来自 ViewController 的 UINavigation 控制器