无法识别的选择发送到实例0x6b61d10(unrecognized selector sent t

2019-09-28 08:14发布

我练的iPhone开发委托,但有一个问题

2012-05-12 21:21:54.279 DelegatePractice [2924:F803] - [secondViewController viewControllers]:无法识别的选择发送到实例0x6b61d10 2012-05-12 21:21:54.281 DelegatePractice [2924:F803] *终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因是: ' - [secondViewController viewControllers]:无法识别的选择发送到实例0x6b61d10' *第一掷调用堆栈:(0x13bb052 0x154cd0a 0x13bcced 0x1321f00 0x1321ce2 0x2596 0x436e1e 0x13bcec9 0x155c2 0x250d54 0x13bcec9 0x155c2 0x1555a 0xbab76 0xbb03f 0xba2fe 0x3aa30 0x3ac56 0x21384 0x14aa9 0x12a5fa9 0x138f1c5 0x12f4022 0x12f290a 0x12f1db4 0x12f1ccb 0x12a4879 0x12a493e 0x12a9b 0x1e98 0x1df5)

我想我成立了代表正确:

delegateViewController.m

- (void)secondViewControllerDidJump:
(secondViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"jump"])
    {
        UINavigationController *navigationController = segue.destinationViewController;
        secondViewController *SecondViewController =[[navigationController viewControllers]objectAtIndex:0];
        SecondViewController.delegate = self;
    }
}

@end

secondViewController.h

@class secondViewController;

@protocol SecondViewControllerDelegate <NSObject>
- (void)secondViewControllerDidJump: (secondViewController *)controller;

@end

@interface secondViewController : UITableViewController

@property (strong,nonatomic) NSArray *choices;
@property (nonatomic, weak) id <SecondViewControllerDelegate> delegate;

- (IBAction)jump:(id)sender;

@end

secondViewController.m

- (IBAction)jump:(id)sender
{
    [self.delegate secondViewControllerDidJump:self];
}

Answer 1:

你的应用程序崩溃,在这些线路:

UINavigationController *navigationController = segue.destinationViewController;
secondViewController *SecondViewController =[[navigationController viewControllers]objectAtIndex:0];

你问navigationControllerviewControllers ,但日志明确指出navigationController 不是一个UINavigationController ,但一个secondViewController ,不给回应选择-viewControllers

确保segue.destinationViewController真正返回UINavigationController



Answer 2:

有同样的问题,我解决了使用:

secondViewController *SecondViewController = segue.destinationViewController;

基本上segue.destinationViewController返回一个视图控制器,因此使用它。



Answer 3:

尝试这个

secondViewController *SecondViewController =[[self.navigationController viewControllers]objectAtIndex:0];


文章来源: unrecognized selector sent to instance 0x6b61d10