【问题标题】:UIPageViewController NSInternalInconsistencyExceptionUIPageViewController NSInternalInconsistencyException
【发布时间】:2014-08-31 17:41:48
【问题描述】:

我正在开发一个简单的UIPageViewController,它有两页。每个页面都是从不同的ViewController 加载的,它们具有特定的xib 文件,名为PViewControllerTViewController

对于PViewController,有三种不同的视图,我们称它们为ABC。当应用程序启动时,PViewController 成功出现,我可以向左滑动看到TViewController,同样没有问题。但是,当我在 PViewController 中并作为对事件的响应时,我将当前视图 A 更改为另一个视图 B,然后向左滑动以转到 @987654330 @,我收到以下异常并且应用程序终止:

*** Assertion failure in -[_UIQueuingScrollView _setWrappedViewAtIndex:withView:], /SourceCache/UIKit_Sim/UIKit-2935.137/_UIQueuingScrollView.m:338
2014-07-10 13:57:23.389 ***** [2012:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unexpected subviews'
*** First throw call stack:
(
    0   CoreFoundation                      0x01fde1e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01c418e5 objc_exception_throw + 44
    2   CoreFoundation                      0x01fde048 +[NSException raise:format:arguments:] + 136
    3   Foundation                          0x018214de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
    4   UIKit                               0x00f3cc14 -[_UIQueuingScrollView _setWrappedViewAtIndex:withView:] + 261
    5   UIKit                               0x00f3d248 -[_UIQueuingScrollView _replaceViews:updatingContents:adjustContentInsets:animated:] + 812
    6   UIKit                               0x00f3d690 -[_UIQueuingScrollView _viewAtIndex:loadingIfNecessary:updatingContents:animated:] + 421
    7   UIKit                               0x00f40c65 __54-[_UIQueuingScrollView _didScrollWithAnimation:force:]_block_invoke + 110
    8   UIKit                               0x00f408de -[_UIQueuingScrollView _didScrollWithAnimation:force:] + 579
    9   UIKit                               0x00f3c452 -[_UIQueuingScrollView layoutSubviews] + 186
    10  UIKit                               0x00970964 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    11  libobjc.A.dylib                     0x01c5382b -[NSObject performSelector:withObject:] + 70
    12  QuartzCore                          0x04be445a -[CALayer layoutSublayers] + 148
    13  QuartzCore                          0x04bd8244 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    14  QuartzCore                          0x04bd80b0 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    15  QuartzCore                          0x04b3e7fa _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    16  QuartzCore                          0x04b3fb85 _ZN2CA11Transaction6commitEv + 393
    17  QuartzCore                          0x04b40258 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    18  CoreFoundation                      0x01fa636e __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    19  CoreFoundation                      0x01fa62bf __CFRunLoopDoObservers + 399
    20  CoreFoundation                      0x01f839eb CFRunLoopRunSpecific + 491
    21  CoreFoundation                      0x01f837eb CFRunLoopRunInMode + 123
    22  GraphicsServices                    0x030285ee GSEventRunModal + 192
    23  GraphicsServices                    0x0302842b GSEventRun + 104
    24  UIKit                               0x00901f9b UIApplicationMain + 1225
    25  *******                             0x000239fd main + 141
    26  libdyld.dylib                       0x02cc1701 start + 1
    27  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

这是我为 UIPageViewController 实现的协议方法:

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    UIViewController *vc;

    if(self.index==1) {
        vc = (PViewController *)[[PViewController alloc] initWithNibName:@"PViewController" bundle:nil];
    }
    self.index--;


    return vc;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    UIViewController *vc;



    if(self.index==0)
        vc = (TViewController *)[[TViewController alloc] initWithNibName:@"TViewController" bundle:nil];

    self.index++;


    return vc;
} //The exception occurs exactly when the app reaches this point.

这就是我在视图控制器中切换视图的方式,简单地说:

self.view = self.B; 

问题:

我无法找到问题所在。我不知道在哪里捕获此异常或导致它的原因是什么?

【问题讨论】:

  • 我正在尝试做这件事,但遇到了完全相同的错误。你弄清楚是什么原因造成的吗?

标签: ios objective-c exception uipageviewcontroller


【解决方案1】:

想通了(至少我的问题是) - 你不应该直接设置 UIViewController 的 self.view,而是使用 self.view.addSubview(viewA) 然后使用

UIView.transitionFromView(self.viewA, toView: self.viewB, duration: 0.5, options: UIViewAnimationOptions.TransitionFlipFromLeft, completion: nil)

【讨论】:

    【解决方案2】:

    我在使用启动时传递的以下参数时偶然发现了同样的错误:

    -UIViewShowAlignmentRects YES
    

    在浪费了 2 天的时间试图在我的 UIPageViewController 中查找错误之后,事实证明,在禁用 XCode 生成的黄色矩形包装后,NSInternalConsistencyException 消失了。

    【讨论】:

    • 非常感谢!难以置信
    • @Michael,XCode generated yellow rectangle wrappers 是什么?
    • @rPragma 当您将描述的参数添加到您的方案时,您会注意到您的视图开始显示与其对齐矩形匹配的黄色边框。
    • 那么我应该在方案(调试和发布)中将UIViewShowAlignmentRects 标志设置为YES 吗?
    • 哦。但我没有使用这个标志。还是谢谢。
    【解决方案3】:

    在我的例子中,这发生在我试图导航到另一个视图控制器中的下一页视图控制器时,该控制器显示为模态

    希望这对其他人有所帮助。

    【讨论】:

    • 我遇到了类似的情况。当我想转到下一个视图控制器时,我的 tableview 中有一个活动的 searchcontroller。转换前的简单关闭解决了问题。
    • 我也有同样的情况,我展示了一个带有样式 overCurrentContext 的 modalView。之后,当我尝试移动到 pageVC 的下一个 VC 时,它崩溃了。
    【解决方案4】:

    我在 Storyboard 中有一个“UIPageViewController”,并且从其中一个页面中,我试图展示新的“UINavigationController”,并附有单独的 xib 中的“Rootviewcontroller”。

    当我删除 xib 并将这个视图控制器作为独立的故事板放置时,它就像一个魅力。

    所以我认为关键是如果两个容器视图都在同一个故事板中,那么它不会给出任何异常或错误。

    let listView = String(describing: ListViewController.self)
    let listViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: listView)
    let navigationController = UINavigationController(rootViewController: listViewController)
    self.present(navigationController, animated: true, completion: nil)
    

    【讨论】:

      【解决方案5】:

      我通过将方向 .forward 更改为 .reverse 来修复此崩溃

      pageViewController?.setViewControllers([viewController], direction: direction, animated: true)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-06
        • 2013-02-14
        • 1970-01-01
        • 2017-09-02
        • 1970-01-01
        相关资源
        最近更新 更多