2017-06-27 12 views
0

コード内に2つのView Controllerの間で転送するために使用しているコードを次に示します。これは動作せず、エラーTHREAD 1信号SIGBARTにつながります。Viewコントローラをプログラムで切り替えることができません

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 
} 

はまた、全体のビューコントローラファイルはここで見ることができます:クラッシュ時にここで

https://paste.ofcode.org/E4PkHFVKXExgxemuDMyU9Kがフルコンソール出力です:

 
2017-06-27 13:57:13.823 College Search[80667:48415876] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: {length = 2, path = 0 - 3}' 
*** First throw call stack: 
(
    0 CoreFoundation      0x0000000104121b0b __exceptionPreprocess + 171 
    1 libobjc.A.dylib      0x0000000103771141 objc_exception_throw + 48 
    2 CoreFoundation      0x000000010418a625 +[NSException raise:format:] + 197 
    3 UIKit        0x0000000105a0881f -[UICollectionView _contentOffsetForScrollingToItemAtIndexPath:atScrollPosition:] + 216 
    4 UIKit        0x0000000105a09250 -[UICollectionView scrollToItemAtIndexPath:atScrollPosition:animated:] + 70 
    5 College Search      0x0000000102d4387f _TFC14College_Search15LogInController8nextPagefT_T_ + 1343 
    6 College Search      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 sendEvent:] + 4114 
    13 UIKit        0x00000001050e27b0 -[UIApplication sendEvent:] + 352 
    14 UIKit        0x000000011543175c -[UIApplicationAccessibility sendEvent:] + 85 
    15 UIKit        0x00000001058c5adc __dispatchPreprocessedEventFromEventQueue + 2926 
    16 UIKit        0x00000001058bda3a __handleEventQueue + 1122 
    17 CoreFoundation      0x00000001040c7c01 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
    18 CoreFoundation      0x00000001040ad0cf __CFRunLoopDoSources0 + 527 
    19 CoreFoundation      0x00000001040ac5ff __CFRunLoopRun + 911 
    20 CoreFoundation      0x00000001040ac016 CFRunLoopRunSpecific + 406 
    21 GraphicsServices     0x0000000109a94a24 GSEventRunModal + 62 
    22 UIKit        0x00000001050c50d4 UIApplicationMain + 159 
    23 College Search      0x0000000102d4ac67 main + 55 
    24 libdyld.dylib      0x00000001070b165d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
(lldb) 
+0

クラッシュを引き起こす正確な行を指摘する必要があり、デバッガコンソールに表示された完全なエラーメッセージを含める必要があります。 – rmaddy

+0

https://pasteboard.co/2hjJTKPPI.png –

+0

投稿したコードにエラーがある場合は、疑わしいビットが1つしか表示されません。 'as! 'を削除してみてください! logloginController'を実行して、 'vc'の値/タイプがあなたが強制していないときの値を確認してください。 –

答えて

0

ああ大丈夫、私が編集を参照してください。今のように3ページあるとしましょう。ページの配列では、ページの番号は0,1,2となります。

ここで、2番目から最後のページにいるとしましょう。それはあなたを1にします、そうですか?次のページに移動しましょう.2です。今、あなたのロジック(あなたの頭の中にある)は、現在のページ=ページ数であれば何かをしていると言っています。このロジックは、基本的には「の最後のページに到達すると、このビューコントローラをロードします」と言っています。

あなたが実際にやっていることは他にもあります。あなたがの配列を持っているので、pages.count = 3です。だからあなたのcurrentPageはそのロジックが動作するためには3に等しくなければなりません。だからあなたのcurrentPage = 2(実際には本当の最後のページです)、currentPage == pages.countの条件をスキップすると、3のインデックスパスを読み込もうとします。それは存在しません

あなたの状態を変えることが必要です。代わりにcurrentPageにの==あなたがそれをする必要がありpages.count:

currentPageに== pages.count - 1

はあなたが後に問題ないはずです。

+0

これは意味がありますが、まだ失敗しています。 –

+0

私はpageControl.currentPage == 0とpageControl.currentPage == 1に変更しますが、それが2に入ればクラッシュします。 –

+0

Hm、nextPage関数ではcurrentPage + = 1をとり、関数の先頭にif文の前に置きます。次に、ScrollViewWillEndDragging関数で、currentPage = pageNumber行をコメントアウトします。それが動作するかどうかを確認する – Eyesofbanquo

関連する問題