PageViewController:SetViewController 使我的应用程序崩溃

PageViewController: SetViewController Crashes my App

当我 运行 我的应用程序时,我注意到 setViewController 方法会导致内存 link 并崩溃 我的 app.I 已经在这个问题上工作了几个小时,并且尝试在网上查找,发现一个 post,其中提到将我的 setViewController 方法放在 dispatch_async(dispatch_get_main_queue) 中,但这会终止应用程序,并且我在控制台中收到以下错误:

2015-12-26 19:41:55.596 Harish Yerra[4663:132796] XPC connection interrupted 2015-12-26 19:41:55.596 Harish Yerra[4663:132797] Terminating since there is no system app.

iPhone 然后完全重新启动并返回主屏幕。

我已经尝试注释掉 setViewController 方法,我的应用程序可以正常加载页面视图控制器的第一页,然后当我滚动到下一页时它再次崩溃并出现以下错误:

* Assertion failure in -[_UIQueuingScrollView _replaceViews:updatingContents:adjustContentInsets:animated:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/_UIQueuingScrollView.m:377 2015-12-26 19:31:55.058 Harish Yerra[4381:123817] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: [views count] == 3' *** First throw call stack: ( 0 CoreFoundation 0x000000010e411e65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x00000001105b0deb objc_exception_throw + 48 2 CoreFoundation 0x000000010e411cca +[NSException raise:format:arguments:] + 106 3 Foundation 0x000000010ea824de -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198 4 UIKit 0x000000010f8d5575 -[_UIQueuingScrollView _replaceViews:updatingContents:adjustContentInsets:animated:] + 289 5 UIKit 0x000000010f8d949d -[_UIQueuingScrollView _didScrollWithAnimation:force:] + 1350 6 UIKit 0x000000010f8d4701 -[_UIQueuingScrollView layoutSubviews] + 176 7 UIKit 0x000000010f1444a3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703 8 QuartzCore 0x000000010ef4959a -[CALayer layoutSublayers] + 146 9 QuartzCore 0x000000010ef3de70 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366 10 QuartzCore 0x000000010ef3dcee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24 11 QuartzCore 0x000000010ef32475 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277 12 QuartzCore 0x000000010ef5fc0a _ZN2CA11Transaction6commitEv + 486 13 QuartzCore 0x000000010ef6e9f4 _ZN2CA7Display11DisplayLink14dispatch_itemsEyyy + 576 14 CoreFoundation 0x000000010e371c84 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION + 20 15 CoreFoundation 0x000000010e371831 __CFRunLoopDoTimer + 1089 16 CoreFoundation 0x000000010e333241 __CFRunLoopRun + 1937 17 CoreFoundation 0x000000010e332828 CFRunLoopRunSpecific + 488 18 GraphicsServices 0x0000000112219ad2 GSEventRunModal + 161 19 UIKit 0x000000010f08d610 UIApplicationMain + 171 20 Harish Yerra 0x000000010d813efd main + 109 21 libdyld.dylib 0x00000001115f092d start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb).

我会提供我的 viewDidLoad 的代码,但如果您需要整个 PageViewControllerDataSource 的代码,请告诉我,我会提供。非常感谢您的帮助。

    override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    self.pageViewController = harishStoryboard.instantiateViewControllerWithIdentifier("HarishPageViewController") as! UIPageViewController
    self.pageViewController.dataSource = self
    let initialViewController = harishStoryboard.instantiateInitialViewController() as! HarishIntroduction
    let viewController = NSArray(object: initialViewController)
    dispatch_async(dispatch_get_main_queue(), {
        self.pageViewController.setViewControllers(viewController as? [UIViewController], direction: .Forward, animated: false, completion: nil)
    })
    self.pageViewController.view.frame = CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height-100)
    self.addChildViewController(pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)
}

如果你想创建一个UIPageViewController?您可以在 Xcode 中找到最详细的信息!只需点击按钮“Page Based Application”而不是“Single View Application”! Apple 已经获得了 Xcode 7.2 中的最新更改,其中包含大量代码!祝你好运!

好的,非常感谢大家的帮助。但是,我想我发现了我的问题所在。我的第一个错误是没有将 setViewControllers 方法放在 dispatch_get_main_queue() 中。另外,this link 帮了我很多忙。它基本上告诉我在 viewController.isKindOfClass 方法上使用 if 语句来确定在 pageViewController beforeViewController 和 [=16= 的数据源方法中正确导航到哪个视图] 方法。

我真的希望这个 post 能帮助其他可能遇到与我相同问题的人。不过还是谢谢你的帮助!