if语句中的performSegueWithIdentifier导致应用程序使用UICollectionView崩溃

时间:2014-12-19 01:33:48

标签: ios swift segue uicollectionviewcell uistoryboardsegue

我有一个UICollectionView和这个方法:

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

在这个方法中,我有这样的说法:

switch indexPath.item {
case 7:
    switch boatTypeSelectedText {
    case "Royal Boat":
        if definitionSelectedText == "Used to carry the pharoah" {
            if let cell = collectionView.cellForItemAtIndexPath(indexPath) {
                cell.backgroundColor = UIColor.greenColor()
            }
        } else {
            self.performSegueWithIdentifier("Game End", sender: self)
            gameOutcomeLabel.text = "You Lost on level \(selectLevelBoatMatchingGame.currentLevel)!"
        }

注意:gameOutComeLabel是segue应发送到的视图控制器的一部分,但该视图控制器与当前视图控制器是同一个类。在else语句中,我创建了一个performSegueWithIdentifier方法。触发此方法时,我在控制台中收到以下错误:

2014-12-18 16:52:39.908 SailingTheNile[31847:20249939] *** Assertion failure in -[SailingTheNile.BoatMatchingGame loadView], /SourceCache/UIKit_Sim/UIKit-3318.16.14/UICollectionViewController.m:166
2014-12-18 16:52:39.912 SailingTheNile[31847:20249939] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UICollectionViewController loadView] loaded the "83r-qs-tQG-view-Zwj-3W-sDw" nib but didn't get a UICollectionView.'
*** First throw call stack:
(
    0   CoreFoundation                      0x002fd946 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x01cd9a97 objc_exception_throw + 44
    2   CoreFoundation                      0x002fd7da +[NSException raise:format:arguments:] + 138
    3   Foundation                          0x00770810 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118
    4   UIKit                               0x012545a8 -[UICollectionViewController loadView] + 597
    5   UIKit                               0x00c67fef -[UIViewController loadViewIfRequired] + 78
    6   UIKit                               0x00c68595 -[UIViewController view] + 35
    7   UIKit                               0x012db707 -[_UIFullscreenPresentationController _setPresentedViewController:] + 75
    8   UIKit                               0x00c3da81 -[UIPresentationController initWithPresentedViewController:presentingViewController:] + 113
    9   UIKit                               0x00c75a61 -[UIViewController _presentViewController:withAnimationController:completion:] + 2102
    10  UIKit                               0x00c785d2 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 345
    11  UIKit                               0x00c78424 -[UIViewController presentViewController:animated:completion:] + 224
    12  UIKit                               0x01140a94 -[UIStoryboardPresentationSegue perform] + 117
    13  UIKit                               0x0112db49 -[UIStoryboardSegueTemplate _perform:] + 217
    14  UIKit                               0x00c6a81c -[UIViewController performSegueWithIdentifier:sender:] + 72
    15  SailingTheNile                      0x00080207 _TFC14SailingTheNile16BoatMatchingGame14collectionViewfS0_FTCSo16UICollectionView24didSelectItemAtIndexPathCSo11NSIndexPath_T_ + 14391
    16  SailingTheNile                      0x00081b99 _TToFC14SailingTheNile16BoatMatchingGame14collectionViewfS0_FTCSo16UICollectionView24didSelectItemAtIndexPathCSo11NSIndexPath_T_ + 89
    17  UIKit                               0x012287e8 -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 591
    18  UIKit                               0x01246022 -[UICollectionView _userSelectItemAtIndexPath:] + 191
    19  UIKit                               0x01246216 -[UICollectionView touchesEnded:withEvent:] + 492
    20  libobjc.A.dylib                     0x01cef7cd -[NSObject performSelector:withObject:withObject:] + 84
    21  UIKit                               0x00cc6254 forwardTouchMethod + 270
    22  UIKit                               0x00cc62c4 -[UIResponder touchesEnded:withEvent:] + 31
    23  libobjc.A.dylib                     0x01cef7cd -[NSObject performSelector:withObject:withObject:] + 84
    24  UIKit                               0x00cc6254 forwardTouchMethod + 270
    25  UIKit                               0x00cc62c4 -[UIResponder touchesEnded:withEvent:] + 31
    26  UIKit                               0x00b6260a -[UIWindow _sendTouchesForEvent:] + 874
    27  UIKit                               0x00b630e5 -[UIWindow sendEvent:] + 791
    28  UIKit                               0x00b28549 -[UIApplication sendEvent:] + 242
    29  UIKit                               0x00b3837e _UIApplicationHandleEventFromQueueEvent + 20690
    30  UIKit                               0x00b0cb19 _UIApplicationHandleEventQueue + 2206
    31  CoreFoundation                      0x002211df __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    32  CoreFoundation                      0x00216ced __CFRunLoopDoSources0 + 253
    33  CoreFoundation                      0x00216248 __CFRunLoopRun + 952
    34  CoreFoundation                      0x00215bcb CFRunLoopRunSpecific + 443
    35  CoreFoundation                      0x002159fb CFRunLoopRunInMode + 123
    36  GraphicsServices                    0x02d0724f GSEventRunModal + 192
    37  GraphicsServices                    0x02d0708c GSEventRun + 104
    38  UIKit                               0x00b108b6 UIApplicationMain + 1526
    39  SailingTheNile                      0x0007546e top_level_code + 78
    40  SailingTheNile                      0x000754ab main + 43
    41  libdyld.dylib                       0x030b1ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

How can I prevent this error from happening and the performSegueWithIdentifier method to work? Let me know if I need to provide any more code. Thanks for the support!

2 个答案:

答案 0 :(得分:0)

您必须在prepareForSegue()中执行此操作。你可以这样做:

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let destination = segue.destinationViewController as YourViewControllerName
    destination.gameOutcomeLabel.text = "AnyText"
}

您要显示的文字必须与发件人一起“传输”或存储为公共变量。

答案 1 :(得分:0)

我在第二个ViewController中做了什么,我创建了第一个ViewController的实例:

class GameOutcome: UIViewController {
var boatMatchingGame: BoatMatchingGame!

然后,在我的第一个ViewController中,我设置了prepareForSegue方法,我这样做了:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    let gameOutcome = segue.destinationViewController as GameOutcome
    gameOutcome.boatMatchingGame = self;

boatMatchingGame是我设置的实例。接下来,在viewDidLoad中,我通过传入我设置的public var的值来更改标签的文本。

override func viewDidLoad() {
    super.viewDidLoad()
    gameOutcomeLabel.text = gameOutcomeText
}

感谢任何人的支持!