问题
I have two buttons in a custom layout collectionView's footer. I want to pass the indexPath of the footerView to destinationView class when user taps the button. I wrote my code in a typical way which I succeeded before. However, in some reason, whenever I tap the button it crashes.
I tried different approaches for each button to accomplish this job but neither worked. I have set destinationView's class to the right class in a storyboard. Please somebody help me. Any help with code example will be very appreciated since English is not my first language. Thank you.
Here's my code
// 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;
}
}
below is header of one of the destination class
#import <UIKit/UIKit.h>
@interface SeeAllPicture : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) NSDictionary *indexPathDic;
@property NSInteger section;
@end
And this is the message when my app crashes
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
)
回答1:
It looks like your SeeAllPicture
view controller is embedded in a UINavigationController
. In this case destinationViewController
on the segue is that navigation controller NOT your SeeAllPicture
view controller.
You probably want the topViewController
of the destinationViewController
UINavigationController *navigationVC = (UINavigationController *)[segue destinationViewController];
SeeAllPicture *destVC = navigationVC.topViewController;
回答2:
as rdelmar says its fine, but to have it crash free you could checko for the Class with isKindOfClass:
. Anyways, just set a breakpoint after
SeeAllPicture *destVC = (SeeAllPicture *)[segue destinationViewController];
and check the console in XCode, here u will se that the class is kind of UINavigationController :)
来源:https://stackoverflow.com/questions/29833284/how-to-pass-indexpath-from-segue-to-destination-view