UIPageViewController Transition Style = Page curl工作正常,Scroll抛出索引越界?

时间:2015-11-03 23:29:51

标签: ios swift uipageviewcontroller

transition style设置为Page Curl(在故事板中)时,我的UIPageViewController工作正常,但当我将其更改为Scroll时,它会因索引超出范围而崩溃。导致这种情况的区别是什么?我无法弄清楚

var times: [String]?


    override func viewDidLoad() {
        super.viewDidLoad()

        self.dataSource = self

        let arrayOfTimes = [Int](0...384).filter { $0 % 3 == 0 }
        times = arrayOfTimes.map { String(format: "%03d", $0) }

        showVC()
    }

    // MARK: UIPageViewControllerDataSource & UIPageViewControllerDelegate
    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        if let currentPageViewController = viewController as? SinglePageViewController, currentTime = currentPageViewController.time {
            let currentIndex = indexOftime(currentTime) //need to get it's index
            return viewControllerAtIndex(currentIndex - 1)
        }
        return nil
    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        if let currentPageViewController = viewController as? SinglePageViewController, currentTime = currentPageViewController.time {
            let currentIndex = indexOftime(currentTime) //need to get it's index
            return viewControllerAtIndex(currentIndex + 1)
        }
        return nil
    }

    func viewControllerAtIndex(index: Int) -> UIViewController? {
        if let time = times?[index] {
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("SinglePageViewController") as! SinglePageViewController
            vc.time = time
            return vc
        }
        return nil
    }

        func indexOftime(time: String) -> Int {
            return times!.indexOf(time)!
        }
    func showVC() {
        if let firstVC = viewControllerAtIndex(0) {
            let viewControllers = [firstVC]
            self.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
        }
    }

enter image description here

fatal error: Array index out of range
(lldb) bt
* thread #1: tid = 0x3d6b22, 0x00000001001f04fc libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> () + 44, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x1001f04fc)
  * frame #0: 0x00000001001f04fc libswiftCore.dylib`function signature specialization <Arg[0] = Exploded, Arg[1] = Exploded, Arg[2] = Dead, Arg[3] = Dead> of Swift._fatalErrorMessage (Swift.StaticString, Swift.StaticString, Swift.StaticString, Swift.UInt) -> () + 44
    frame #1: 0x00000001000d9810 libswiftCore.dylib`generic specialization <Swift.String> of Swift.Array.subscript.getter : (Swift.Int) -> A + 92
    frame #2: 0x000000010001b994 WXModels`WXModels.WxModelPageViewController.viewControllerAtIndex (index=-1, self=0x000000012500da00)(Swift.Int) -> Swift.Optional<__ObjC.UIViewController> + 152 at WxModelPageViewController.swift:50
    frame #3: 0x000000010001b570 WXModels`WXModels.WxModelPageViewController.pageViewController (pageViewController=0x000000012500da00, viewController=0x0000000124674840, self=0x000000012500da00)(__ObjC.UIPageViewController, viewControllerBeforeViewController : __ObjC.UIViewController) -> Swift.Optional<__ObjC.UIViewController> + 460 at WxModelPageViewController.swift:36
    frame #4: 0x000000010001b630 WXModels`@objc WXModels.WxModelPageViewController.pageViewController (WXModels.WxModelPageViewController)(__ObjC.UIPageViewController, viewControllerBeforeViewController : __ObjC.UIViewController) -> Swift.Optional<__ObjC.UIViewController> + 92 at WxModelPageViewController.swift:0
    frame #5: 0x00000001892aa03c UIKit`-[UIPageViewController _viewControllerBefore:viewController:] + 108
    frame #6: 0x00000001892afb08 UIKit`-[UIPageViewController _queuingScrollView:viewBefore:view:] + 268
    frame #7: 0x00000001892afbf0 UIKit`-[UIPageViewController queuingScrollView:viewBeforeView:] + 64
    frame #8: 0x0000000189359c40 UIKit`-[_UIQueuingScrollView _viewBefore:view:] + 108
    frame #9: 0x000000018935a3a0 UIKit`-[_UIQueuingScrollView _requestViewForIndex:] + 296
    frame #10: 0x000000018935a728 UIKit`-[_UIQueuingScrollView _viewAtIndex:loadingIfNecessary:updatingContents:animated:] + 368
    frame #11: 0x000000018935cb84 UIKit`-[_UIQueuingScrollView _adjustContentInsets] + 216
    frame #12: 0x000000018935a234 UIKit`-[_UIQueuingScrollView _replaceViews:updatingContents:adjustContentInsets:animated:] + 828
    frame #13: 0x000000018935a7dc UIKit`-[_UIQueuingScrollView _viewAtIndex:loadingIfNecessary:updatingContents:animated:] + 548
    frame #14: 0x000000018935d930 UIKit`__54-[_UIQueuingScrollView _didScrollWithAnimation:force:]_block_invoke + 124
    frame #15: 0x000000018935d74c UIKit`-[_UIQueuingScrollView _didScrollWithAnimation:force:] + 880
    frame #16: 0x00000001893592b0 UIKit`-[_UIQueuingScrollView layoutSubviews] + 196
    frame #17: 0x0000000188b477ac UIKit`-[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 644
    frame #18: 0x0000000188346b58 QuartzCore`-[CALayer layoutSublayers] + 148
    frame #19: 0x0000000188341764 QuartzCore`CA::Layer::layout_if_needed(CA::Transaction*) + 292
    frame #20: 0x0000000188341624 QuartzCore`CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 32
    frame #21: 0x0000000188340cc0 QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 252
    frame #22: 0x0000000188340a08 QuartzCore`CA::Transaction::commit() + 512
    frame #23: 0x000000018833a0f8 QuartzCore`CA::Transaction::observer_callback(__CFRunLoopObserver*, unsigned long, void*) + 80
    frame #24: 0x000000018356fbd0 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
    frame #25: 0x000000018356d974 CoreFoundation`__CFRunLoopDoObservers + 372
    frame #26: 0x000000018349ccc0 CoreFoundation`CFRunLoopRunSpecific + 416
    frame #27: 0x000000018ea20088 GraphicsServices`GSEventRunModal + 180
    frame #28: 0x0000000188bb4ffc UIKit`UIApplicationMain + 204
    frame #29: 0x000000010001db90 WXModels`main + 136 at AppDelegate.swift:12
    frame #30: 0x00000001992a68b8 libdyld.dylib`start + 4
(lldb) 

1 个答案:

答案 0 :(得分:1)

pageViewController(_:viewControllerBeforeViewController:)(第3帧)中,即使viewControllerAtIndex(currentIndex - 1)为零,也会调用currentIndex(第2帧),因此您将-1传递给{{1 }}。你应该在viewControllerAtIndex(_:)中检查一下。这样的事情应该做:

viewControllerAtIndex(_:)