gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:30:35 24 4
gpt4 key购买 nike

transition style设置为Page Curl(在 Storyboard中)时,我的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)

最佳答案

pageViewController(_:viewControllerBeforeViewController:)(第 3 帧)中,您调用 viewControllerAtIndex(currentIndex - 1)(第 2 帧),即使 currentIndex 为零,因此您将 -1 传递给 viewControllerAtIndex(_:)。您应该在 viewControllerAtIndex(_:) 中检查它。应该这样做:

func viewControllerAtIndex(index: Int) -> UIViewController? {
if index < 0 || index >= (times?.count ?? 0) {
return nil
}

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
}

关于ios - UIPageViewController Transition Style = Page curl 工作正常,Scroll 抛出索引越界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33511080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com