如何从segue传递indexPath到目标视图?

我有一个自定义布局collectionView的页脚中的两个button。 我想在用户点击button时将footerView的indexPath传递给destinationView类。 我以我以前成功的典型方式编写我的代码。 但是,由于某种原因,只要我点击button,就会崩溃。

我尝试了不同的方法来完成这项工作,但都没有成功。 我已经将destinationView的类设置为故事板中的正确的类。 请有人帮助我。 任何帮助代码示例将非常赞赏,因为英语不是我的第一语言。 谢谢。

这是我的代码

// perform segue passing indexPath information - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // approach 1 : pass dictionary if([[segue identifier] isEqualToString:@"SeeAllPictures"]) { Footer *footerView = (Footer *)((UIButton *)sender).superview; // debug code : working well NSLog(@"perform segue from indexpath {%ld - %ld}", (long) footerView.indexPath.section, (long) footerView.indexPath.row); SeeAllPicture *destVC = (SeeAllPicture *)[segue destinationViewController]; NSDictionary *indexPathDic = @{@"key1":footerView.indexPath}; // crashes at below line destVC.indexPathDic = indexPathDic; } // approach 2 : pass indexPath.section, the NSInteger value else if([[segue identifier] isEqualToString:@"GoToDiary"]) { Footer *footerView = (Footer *)((UIButton *)sender).superview; // debug code : working well NSLog(@"perform segue from indexpath {%ld - %ld}", (long) footerView.indexPath.section, (long) footerView.indexPath.row); Diary *destVC = (Diary *)[segue destinationViewController]; // crashes at below line destVC.section = footerView.indexPath.section; } } 

下面是目标类之一的标题

 #import <UIKit/UIKit.h> @interface SeeAllPicture : UIViewController @property (strong, nonatomic) IBOutlet UILabel *titleLabel; @property (strong, nonatomic) NSDictionary *indexPathDic; @property NSInteger section; @end 

这是我的应用程序崩溃时的消息

 Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setIndexPathDic:]: unrecognized selector sent to instance 0x7ff8c8c4f140' *** First throw call stack: ( 0 CoreFoundation 0x000000010f7f7a75 __exceptionPreprocess + 165 1 libobjc.A.dylib 0x000000010f490bb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010f7fed1d - [NSObject(NSObject) doesNotRecognizeSelector:] + 205 3 CoreFoundation 0x000000010f7569dc ___forwarding___ + 988 4 CoreFoundation 0x000000010f756578 _CF_forwarding_prep_0 + 120 5 InfanTree 0x000000010ea1113b - [ViewController prepareForSegue:sender:] + 635 6 UIKit 0x000000011037e3cc - [UIStoryboardSegueTemplate _perform:] + 151 7 UIKit 0x000000010fe2ba22 - [UIApplication sendAction:to:from:forEvent:] + 75 8 UIKit 0x000000010ff32e50 -[UIControl _sendActionsForEvents:withEvent:] + 467 9 UIKit 0x000000010ff3221f -[UIControl touchesEnded:withEvent:] + 522 10 UIKit 0x00000001101d9e80 _UIGestureRecognizerUpdate + 9487 11 UIKit 0x000000010fe71856 -[UIWindow _sendGesturesForEvent:] + 1041 12 UIKit 0x000000010fe72483 -[UIWindow sendEvent:] + 667 13 UIKit 0x000000010fe3efb1 - [UIApplication sendEvent:] + 246 14 UIKit 0x000000010fe4c227 _UIApplicationHandleEventFromQueueEvent + 17700 15 UIKit 0x000000010fe2723c _UIApplicationHandleEventQueue + 2066 16 CoreFoundation 0x000000010f72cc91 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 17 CoreFoundation 0x000000010f722b5d __CFRunLoopDoSources0 + 269 18 CoreFoundation 0x000000010f722194 __CFRunLoopRun + 868 19 CoreFoundation 0x000000010f721bc6 CFRunLoopRunSpecific + 470 20 GraphicsServices 0x0000000112950a58 GSEventRunModal + 161 21 UIKit 0x000000010fe2a580 UIApplicationMain + 1282 22 InfanTree 0x000000010ea1b7c3 main + 115 23 libdyld.dylib 0x0000000113211145 start + 1 ) 

它看起来像你的SeeAllPicture视图控制器embedded在一个UINavigationController 。 在这种情况下segue上的destinationViewController是导航控制器而不是您的SeeAllPicture视图控制器。

您可能需要destinationViewControllertopViewController

 UINavigationController *navigationVC = (UINavigationController *)[segue destinationViewController]; SeeAllPicture *destVC = navigationVC.topViewController; 

正如rdelmar说的那样好,但为了让它免费崩溃,你可以使用isKindOfClass检查Class。 无论如何,只需在SeeAllPicture *destVC = (SeeAllPicture *)[segue destinationViewController];之后设置一个断点SeeAllPicture *destVC = (SeeAllPicture *)[segue destinationViewController]; 并检查在XCode控制台,在这里你会认为该类是一种UINavigationController 🙂