2014-05-21 74 views
0

我正在使用此代码将对象传递给另一个视图控制器。将数据从VC传递到VC 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传递给DetailedViewController中的_clipping属性。

+0

你的第一个viewController和你的DetailedViewController之间有导航控制器吗? –

+0

这就是我的故事板设置:http://i.imgur.com/fQPQUGD.png – Clip

+0

你能告诉我们关于你的segue的细节吗?它是从你的第一个viewController开始还是从你的单元开始的? –

回答

1

使用下面的代码,如果没有导航控制器你的两个viewControllers BETWEEN:

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

仅使用,如果有一个导航控制器你的两个viewControllers之间的下面的代码:

-(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]; 
    } 
} 
+0

您发布的代码与我发布的代码相同,但仍然会导致发生异常。 – Clip

+0

我提出的两段代码是不同的! –

0

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

相关问题