将数据从VC传递到VC segue

时间:2014-05-21 00:50:15

标签: ios objective-c uiviewcontroller segue

我正在使用此代码将对象传递给另一个视图控制器。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"Detail"]){
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        DetailedViewController *controller = (DetailedViewController *)navController.topViewController;
        controller.clipping = [pasteBoardArray objectAtIndex:_row];
    }
}

在线:

DetailedViewController *controller = (DetailedViewController *)navController.topViewController;

我得到一个冗长的例外

2014-05-20 18:46:39.752 SWTableViewCell[8370:60b] -[DetailedViewController topViewController]: unrecognized selector sent to instance 0x10c7169e0
2014-05-20 18:46:39.756 SWTableViewCell[8370:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DetailedViewController topViewController]: unrecognized selector sent to instance 0x10c7169e0'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000102667495 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001016c699e objc_exception_throw + 43
    2   CoreFoundation                      0x00000001026f865d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000102658d8d ___forwarding___ + 973
    4   CoreFoundation                      0x0000000102658938 _CF_forwarding_prep_0 + 120
    5   SWTableViewCell                     0x000000010000d91c -[ViewController prepareForSegue:sender:] + 204
    6   UIKit                               0x00000001004f4c73 -[UIStoryboardSegueTemplate _perform:] + 134
    7   SWTableViewCell                     0x000000010000c4be -[ViewController tableView:didSelectRowAtIndexPath:] + 350
    8   SWTableViewCell                     0x00000001000089ae -[SWTableViewCell selectCell] + 798
    9   SWTableViewCell                     0x000000010000864b -[SWTableViewCell scrollViewTapped:] + 171
    10  UIKit                               0x0000000100362fc2 _UIGestureRecognizerSendActions + 188
    11  UIKit                               0x0000000100361f28 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 357
    12  UIKit                               0x00000001003662d9 ___UIGestureRecognizerUpdate_block_invoke + 53
    13  UIKit                               0x0000000100366261 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 257
    14  UIKit                               0x000000010035e337 _UIGestureRecognizerUpdate + 93
    15  UIKit                               0x0000000100072a15 -[UIWindow _sendGesturesForEvent:] + 928
    16  UIKit                               0x00000001000736d4 -[UIWindow sendEvent:] + 909
    17  UIKit                               0x000000010004b29a -[UIApplication sendEvent:] + 211
    18  UIKit                               0x0000000100038aed _UIApplicationHandleEventQueue + 9579
    19  CoreFoundation                      0x00000001025f6d21 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    20  CoreFoundation                      0x00000001025f65f2 __CFRunLoopDoSources0 + 242
    21  CoreFoundation                      0x000000010261246f __CFRunLoopRun + 767
    22  CoreFoundation                      0x0000000102611d83 CFRunLoopRunSpecific + 467
    23  GraphicsServices                    0x00000001025c6f04 GSEventRunModal + 161
    24  UIKit                               0x000000010003ae33 UIApplicationMain + 1010
    25  SWTableViewCell                     0x0000000100002763 main + 115
    26  libdyld.dylib                       0x000000010607f5fd start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

作为附注,我在UINavigationBarController内完成了这一切。我需要做的就是将objectAtIndex:_row传递给_clipping中的DetailedViewController属性。

2 个答案:

答案 0 :(得分:1)

如果两个视图控制器之间没有导航控制器,请使用以下代码:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"Detail"]){
        DetailedViewController *controller = (DetailedViewController *)[segue destinationViewController];
        controller.clipping = [pasteBoardArray objectAtIndex:_row];
        /* ... */
    }
}

仅当两个视图控制器之间有一个导航控制器时,才使用以下代码:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"Detail"]){
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        DetailedViewController *controller = (DetailedViewController *)navController.topViewController;
        controller.clipping = [pasteBoardArray objectAtIndex:_row];
        /* ... */
    }
}

答案 1 :(得分:0)

您发布的错误消息表明您的segue的目标视图控制器不是您认为的导航控制器。您应该记录目标视图控制器的类以查看它是什么。