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

时间:2015-04-23 20:06:00

标签: ios objective-c segue nsindexpath

我在自定义布局collectionView的页脚中有两个按钮。我想在用户点击按钮时将footerView的indexPath传递给destinationView类。 我以一种我之前成功的典型方式编写了代码。 但是,出于某种原因,每当我点击按钮时它都会崩溃。

我为每个按钮尝试了不同的方法来完成这项工作,但都没有奏效。我已将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
)

2 个答案:

答案 0 :(得分:1)

您的SeeAllPicture视图控制器似乎嵌入了UINavigationController。在这种情况下,segue上的destinationViewController是导航控制器而不是您的SeeAllPicture视图控制器。

您可能需要topViewController

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

答案 1 :(得分:0)

正如rdelmar所说的那样好,但为了让它免于崩溃你可以用isKindOfClass:来检查班级。无论如何,只需设置一个断点  SeeAllPicture *destVC = (SeeAllPicture *)[segue destinationViewController]; 并检查XCode中的控制台,在这里你会发现该类是UINavigationController的类:)