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

How to pass indexPath from segue to destination view?(如何将indexPath从段传递到目标视图?)

本文介绍了如何将indexPath从段传递到目标视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自定义布局集合View的页脚中有两个按钮。当用户点击按钮时,我希望将footerView的indexPath传递给estinationView类。 我以一种我以前成功过的典型方式编写代码。 然而,由于某种原因,每当我点击按钮时,它就会崩溃。

我为每个按钮尝试了不同的方法来完成这项工作,但都不起作用。我已经在情节提要中将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视图控制器似乎嵌入在UINavigationController中。在本例中,段上的destinationViewController是导航控制器,而不是您的SeeAllPicture视图控制器。

您可能需要destinationViewControllertopViewController

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

这篇关于如何将indexPath从段传递到目标视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何将indexPath从段传递到目标视图?

基础教程推荐