在 iOS 13 上推送新视图后后退按钮崩溃
Posted
技术标签:
【中文标题】在 iOS 13 上推送新视图后后退按钮崩溃【英文标题】:Back button crashes after pushing a new view on iOS 13 【发布时间】:2019-08-28 11:16:22 【问题描述】:我有一个在 ios 12 上完美运行的应用程序。我想在 iOS 13、XCode 11 上进行测试。我使用情节提要导航 segue 将一个视图控制器推送到另一个视图控制器。当我在第二页上按下后退按钮(默认后退按钮)时,应用程序崩溃并在下面产生错误。它发生在每个导航页面中,甚至是空视图。
2019-08-28 14:10:48.632540+0300 App Name[28453:400531] *** Assertion failure in -[UINavigationController _popNavigationBar:item:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3899.13.11/UINavigationController.m:8520
2019-08-28 14:10:48.642352+0300 App Name[28453:400531] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Override of -navigationBar:shouldPopItem: returned YES after manually popping a view controller (navigationController=<UINavigationController: 0x7fa22f0de000>)'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff23afdbde __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff5015cb20 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23afd958 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff255506f5 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 UIKitCore 0x00007fff46812310 -[UINavigationController _popNavigationBar:item:] + 379
5 UIKitCore 0x00007fff46592209 -[UINavigationBar _sendNavigationPopForBackBarButtonItem:] + 277
6 UIKitCore 0x00007fff465bf771 -[_UINavigationBarContentView __backButtonAction:] + 58
7 UIKitCore 0x00007fff46f0abc1 -[UIApplication sendAction:to:from:forEvent:] + 83
8 UIKitCore 0x00007fff468fc3e5 -[UIControl sendAction:to:forEvent:] + 223
9 UIKitCore 0x00007fff468fc72f -[UIControl _sendActionsForEvents:withEvent:] + 398
10 UIKitCore 0x00007fff468fc892 -[UIControl _sendActionsForEvents:withEvent:] + 753
11 UIKitCore 0x00007fff468fb68e -[UIControl touchesEnded:withEvent:] + 481
12 UIKitCore 0x00007fff46f450c7 -[UIWindow _sendTouchesForEvent:] + 2604
13 UIKitCore 0x00007fff46f469ce -[UIWindow sendEvent:] + 4596
14 UIKitCore 0x00007fff46f2204f -[UIApplication sendEvent:] + 356
15 UIKitCore 0x00007fff46fa18c0 __dispatchPreprocessedEventFromEventQueue + 6847
16 UIKitCore 0x00007fff46fa4386 __handleEventQueueInternal + 5980
17 CoreFoundation 0x00007fff23a60ac1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18 CoreFoundation 0x00007fff23a609ec __CFRunLoopDoSource0 + 76
19 CoreFoundation 0x00007fff23a601c4 __CFRunLoopDoSources0 + 180
20 CoreFoundation 0x00007fff23a5aecf __CFRunLoopRun + 1263
21 CoreFoundation 0x00007fff23a5a6b6 CFRunLoopRunSpecific + 438
22 GraphicsServices 0x00007fff38016bb0 GSEventRunModal + 65
23 UIKitCore 0x00007fff46f0990f UIApplicationMain + 1621
24 App Name 0x000000010691b810 main + 112
25 libdyld.dylib 0x00007fff50fe1cf5 start + 1
26 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
【问题讨论】:
您是否覆盖了 UINavigationBar 的shouldPopItem
代表?
我刚刚添加了 UINavigationControllerDelegate、UINavigationBarDelegate。但是 shouldPopItem 没有被调用。
嘿,你找到解决办法了吗?
【参考方案1】:
创建UINavigationController
的自定义类,然后添加UINavigationBarDelegate
如下并将该类分配给您的UINavigationController
class MyNavigationController: UINavigationController
override func viewDidLoad()
super.viewDidLoad()
self.navigationBar.delegate = self
extension MyNavigationController : UINavigationBarDelegate
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool
return true
【讨论】:
现在它不会崩溃了。但不弹出视图控制器【参考方案2】:我不知道这对你来说是否仍然是个问题,但我有类似的事情(实际上我的问题是它在 ios13 上完美运行,但在 ios12 上后退按钮不起作用)。
错误告诉你不能手动弹出视图控制器然后返回true(即是的你应该弹出视图控制器)所以如果你手动弹出则需要返回false
override public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool
popViewController(animated: true)
return false
【讨论】:
以上是关于在 iOS 13 上推送新视图后后退按钮崩溃的主要内容,如果未能解决你的问题,请参考以下文章
使用 UINavigationController 推送两个视图后后退按钮消失