【问题标题】:Not able to Switch between View controllers Programatically无法以编程方式在视图控制器之间切换
【发布时间】:2017-11-30 20:23:46
【问题描述】:

下面显示的是我用来在我的代码中的两个视图控制器之间传输的代码。这不起作用并导致错误 THREAD 1 Signal SIGBART。

 func nextPage() {
    //we are on the last page
    if pageControl.currentPage == pages.count {
        /*moveControlConstraintsOffScreen()

        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: {
            self.view.layoutIfNeeded()
        }, completion: nil)
        */
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "logloginControllerID") as! logloginController
        self.present(vc, animated: true, completion:nil)
    }

    //second last page
    if pageControl.currentPage == pages.count {
       return
    }

    let indexPath = IndexPath(item: pageControl.currentPage + 1, section: 0)
    collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    pageControl.currentPage += 1
}

这是崩溃时的完整控制台输出:

2017-06-27 13:57:13.823 College Search[80667:48415876] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“尝试滚动到无效索引路径:{长度 = 2,路径 = 0 - 3}' *** 首先抛出调用堆栈: ( 0 核心基础 0x0000000104121b0b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x0000000103771141 objc_exception_throw + 48 2 核心基础 0x000000010418a625 + [NSException raise:format:] + 197 3 UIKit 0x0000000105a0881f -[UICollectionView _contentOffsetForScrollingToItemAtIndexPath:atScrollPosition:] + 216 4 UIKit 0x0000000105a09250-[UICollectionView scrollToItemAtIndexPath:atScrollPosition:animated:] + 70 5 大学搜索 0x0000000102d4387f _TFC14College_Search15LogInController8nextPagefT_T_ + 1343 6 大学搜索 0x0000000102d43a32 _TToFC14College_Search15LogInController8nextPagefT_T_ + 34 7 UIKit 0x00000001050c6d22-[UIApplication sendAction:to:from:forEvent:] + 83 8 UIKit 0x000000010524b25c-[UIControl sendAction:to:forEvent:] + 67 9 UIKit 0x000000010524b577-[UIControl_sendActionsForEvents:withEvent:] + 450 10 UIKit 0x000000010524a4b2-[UIControl touchesEnded:withEvent:] + 618 11 UIKit 0x000000010513449a-[UIWindow_sendTouchesForEvent:] + 2707 12 UIKit 0x0000000105135bb0 -[UIWindow 发送事件:] + 4114 13 UIKit 0x00000001050e27b0 -[UIApplication 发送事件:] + 352 14 UIKit 0x000000011543175c-[UIApplicationAccessibility sendEvent:] + 85 15 UIKit 0x00000001058c5adc __dispatchPreprocessedEventFromEventQueue + 2926 16 UIKit 0x00000001058bda3a __handleEventQueue + 1122 17 核心基础 0x00000001040c7c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 18 核心基础 0x00000001040ad0cf __CFRunLoopDoSources0 + 527 19 核心基础 0x00000001040ac5ff __CFRunLoopRun + 911 20 核心基础 0x00000001040ac016 CFRunLoopRunSpecific + 406 21 图形服务 0x0000000109a94a24 GSEventRunModal + 62 22 UIKit 0x00000001050c50d4 UIApplicationMain + 159 23 大学搜索 0x0000000102d4ac67 主要 + 55 24 libdyld.dylib 0x00000001070b165d 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止 (lldb)

【问题讨论】:

  • 您需要指出导致崩溃的确切行,并且需要包含调试器控制台中显示的完整错误消息。
  • 如果错误出现在您发布的代码中,我只看到一个可能可疑的位。尝试删除as! logloginController,看看你不强制时vc的值/类型是什么。
  • 删除为!导致构建应用程序的尝试失败。但是,我能够使用调试器找到整个错误消息。它指出:libc++abi.dylib:以 NSException 类型的未捕获异常终止
  • SIGABRT 一般表示程序抛出了异常。调试器控制台中应该有更多信息。您需要编辑您的问题以包含该信息。此外,您可以在 objc_exception_throw 中设置断点以获取引发异常的堆栈跟踪。

标签: ios swift uicollectionview


【解决方案1】:

哦,好的,我看到了编辑。假设您现在有 3 页。在页面数组中,您的页面编号为 0、1、2。

现在假设您在倒数第二页。这让你在1,对吧?让我们转到下一页,即 2。现在,您的逻辑(在您的脑海中)是说如果当前页面 = pages.count 则执行某些操作。这个逻辑用基本术语说“一旦我到达 last 页面就加载这个视图控制器”。

你实际上在做的是另一件事。 pages.count = 3 因为您在该数组中有 3 个项目。因此,您的 currentPage 必须等于 3 才能使该逻辑起作用。因此,当您的 currentPage = 2(实际上是真正的最后一页)时,您会跳过 currentPage == pages.count 的条件,最终尝试为 3 加载索引路径。这不存在

所以你需要做的是改变你的状况。而不是 currentPage == pages.count 你需要做它:

currentPage == pages.count - 1

之后你应该会没事的。

【讨论】:

  • 这很有意义,但仍然失败。
  • 我将其更改为 pageControl.currentPage == 0 和 pageControl.currentPage == 1,但如果我输入 2,它就会崩溃。
  • 嗯,在 nextPage 函数中取 currentPage += 1 并将其放在 if 语句之前的函数顶部。然后在 ScrollViewWillEndDragging 函数中注释掉 currentPage = pageNumber 这一行。看看是否有效
【解决方案2】:
func nextPage() {
    //we are on the last page
    if pageControl.currentPage == pages.count {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "logloginControllerID") as! logloginController
        self.present(vc, animated: true, completion:nil)
    }else{
        let indexPath = IndexPath(item: pageControl.currentPage + 1, section: 0)
        collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
        pageControl.currentPage += 1
    }
}

【讨论】:

    猜你喜欢
    • 2010-10-29
    • 2014-09-30
    • 2015-01-20
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多