[自定义导航控制器 popToViewController:transition:] 中的断言失败



我想弹出视图控制器,直到所需的视图控制器位于导航堆栈的顶部。

我是这样做的:

UIViewController *aViewController = [self.navigationController.viewControllers objectAtIndex:lViewControllerIndex];
[self.navigationController popToViewController:aViewController
                                                animated:YES];

从调试器中,我可以看到aViewController <MainViewController: 0x79ea3b10>self.navigationController.viewControllers

<MainViewController: 0x79ea3b10>,
<FirstViewController: 0x79eb2630>,
<SecondViewController: 0x7b258f10>

目前我在SecondViewController,我想回到MainViewController

但它崩溃了,崩溃消息如下:

***** Assertion failure in -[CustomNavigationController popToViewController:transition:], /SourceCache/UIKit_Sim/UIKit-2935.137/UINavigationController.m:4912**

如何通过弹出多个视图控制器来正确返回?

更新 [1]:我不清楚,我不需要如何弹出到根视图控制器的方法,我需要一种方法如何弹出多个视图控制器。上面,这只是一个从SecondViewControllerMainViewController的例子

更新 [2]:

堆栈跟踪:

    0   CoreFoundation                      0x030a81e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x02e218e5 objc_exception_throw + 44
    2   CoreFoundation                      0x030a8048 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x00c7d4de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x01a45ab8 -[UINavigationController popToViewController:transition:] + 918
    5   UIKit                               0x01a4571d -[UINavigationController popToViewController:animated:] + 56
    6   Isi For You                         0x000a14b7 -[DetailViewController breadcrumbItemPressedAtIndex:] + 327
    7   Isi For You                         0x000c4460 -[ListHeaderViewController breadcrumbView:didTapItemAtIndex:] + 144
    8   Isi For You                         0x00094772 -[BTBreadcrumbView didTapItemAtIndex:] + 162
    9   Isi For You                         0x0009480f -[BTBreadcrumbView tapItemButton:] + 143
    10  libobjc.A.dylib                     0x02e33880 -[NSObject performSelector:withObject:withObject:] + 77
    11  UIKit                               0x018fe3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
    12  UIKit                               0x018fe345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
    13  UIKit                               0x019ffbd1 -[UIControl sendAction:to:forEvent:] + 66
    14  UIKit                               0x019fffc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
    15  UIKit                               0x019ff243 -[UIControl touchesEnded:withEvent:] + 641
    16  UIKit                               0x0193dddd -[UIWindow _sendTouchesForEvent:] + 852
    17  UIKit                               0x0193e9d1 -[UIWindow sendEvent:] + 1117
    18  UIKit                               0x019105f2 -[UIApplication sendEvent:] + 242
    19  Isi For You                         0x00434244 -[CustomUIApplication sendEvent:] + 100
    20  UIKit                               0x018fa353 _UIApplicationHandleEventQueue + 11455
    21  CoreFoundation                      0x0303177f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    22  CoreFoundation                      0x0303110b __CFRunLoopDoSources0 + 235
    23  CoreFoundation                      0x0304e1ae __CFRunLoopRun + 910
    24  CoreFoundation                      0x0304d9d3 CFRunLoopRunSpecific + 467
    25  CoreFoundation                      0x0304d7eb CFRunLoopRunInMode + 123
    26  GraphicsServices                    0x042cc5ee GSEventRunModal + 192
    27  GraphicsServices                    0x042cc42b GSEventRun + 104
    28  UIKit                               0x018fcf9b UIApplicationMain + 1225
    29  Isi For You                         0x000449e2 main + 82
    30  libdyld.dylib                       0x038796d9 start + 1
)

当您尝试弹出到的视图控制器不在导航堆栈中时,通常会发生此错误。尽管在您的情况下,您似乎查看控制器实际上在那里......您可以使用popToRootViewControllerAnimated:弹出到您的"根"。

[self.navigationController popToRootViewControllerAnimated:YES];

如果您尝试在没有动画的情况下弹出,它将起作用。当尝试弹出多个动画视图控制器时,我遇到了这个问题。

如果要弹出 n 个视图控制器,直到 n-1 个弹出而没有动画,然后弹出最后一个带有动画。

您可以使用

- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated

UINavigationController上的 API 可以在多个堆栈位置跳跃之间跳跃,或者实际上完全替换堆栈。

从文档

要放置在堆栈中的视图控制器。从前到后的顺序 此数组中的控制器表示新的自下而上的顺序 导航堆栈中的控制器。因此,最后添加的项目 到数组成为导航堆栈的顶部项。

因此,使用 viewControllers 属性恢复当前堆栈,根据需要进行变异(在您的情况下,您将采用原始堆栈的子数组),然后将其转储回导航控制器。

相关内容

最新更新