【问题标题】:ios: NSInvalidArgumentException, unrecognised selector on prepareForSegueios:NSInvalidArgumentException,prepareForSegue 上无法识别的选择器
【发布时间】:2015-08-04 12:28:25
【问题描述】:

情况:

我有三个视图,分别名为 LogInView、CategoryRegistrationView 和 BlueToothCheckerView。 LogInView 和 CategoryRegistrationView 都可以继续使用 BlueToothCheckerView。

BlueToothCheckerView 本身可以分为两个不同的视图,具体取决于 LogInView 或 CategoryRegistrationView 设置的字符串。

LogInView 可以很好地做到这一点。用户可以从 LogInView 登录,检查他的蓝牙连接是否在 BlueToothCheckerView 中打开,然后被重定向到他不会看到的视图,如果他从 CategoryRegistrationView 进来。

CategoryRegistrationView 有问题。

问题:

单击按钮将我带到BlueToothView,出现此错误:

[BlueToothCheckerViewController setFromSegue:]: unrecognized selector sent to instance 0x7f8c935a4f80

网上很多帖子都说是因为BlueToothCheckerView没有设置为xib BlueToothView的Custom Class,但确实是。

代码(我们将专注于 CategoryRegistrationView 和 BlueToothCheckerView 以保持简短):

CategoryRegistrationViewController.h - 问题段:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    NSLog(@"Segue Identifier: %@", [segue identifier]);

    if ([[segue identifier] isEqualToString:@"CategoryRegistrationToBlueToothCheckerSegue"]) {
        BlueToothCheckerView *btcv = [segue destinationViewController];
        btcv.fromSegue = @"CategoryRegistrationToBlueToothCheckerSegue"; // <-- Error here
        NSLog(@"Something wrong here");
    }
}

注意:LogInViewController 的代码完全相同,只是将 btvc.fromSegue 设置为“LoginToBlueToothCheckerSegue”。

BlueToothCheckerView.h

#import <UIKit/UIKit.h>

@protocol BlueToothCheckerViewDelegate <NSObject>

@required
- (void)skipPressed:(id)sender;
@end

@interface BlueToothCheckerView : UIView

@property (weak, nonatomic) id<BlueToothCheckerViewDelegate> delegate;
@property (weak, nonatomic) IBOutlet UIButton *SkipButton;
@property (nonatomic) NSString* fromSegue;

- (IBAction)skipPressed:(id)sender;

@end

BlueToothCheckerViewController.h

#import "BaseUIViewController.h"
#import "BlueToothCheckerView.h"
#import "Constants.h"
#import <CoreBluetooth/CoreBluetooth.h>

@interface BlueToothCheckerViewController : BaseUIViewController <BlueToothCheckerViewDelegate,CBCentralManagerDelegate>

@property (weak, nonatomic) BlueToothCheckerView *btcv;

@end

BlueToothCheckerViewBlueToothCheckerViewController 至少都显示字符串fromSegue

这是一个如何使用 fromSegue 的示例,在BlueToothCheckerViewController.m

- (void)skipPressed:(id)sender {
    NSLog(@"Bluetooth already on");

    if ([self.btcv.fromSegue isEqual: @"LoginToBlueToothCheckerSegue"]){
        [self performSegueWithIdentifier:@"BlueToothCheckerToDashboardSegue" sender:self];
    }
    else if ([self.btcv.fromSegue isEqual: @"CategoryRegistrationToBlueToothCheckerSegue"]){
        [self performSegueWithIdentifier:@"BlueToothCheckerToWalkthroughSegue" sender:self];
    }
}

我尝试了什么:

我尝试删除有问题的 segue,然后重新添加它。它没有太多。我还注释掉了 btcv.fromSegue = @"CategoryRegistrationToBlueToothCheckerSegue"; 行,并确认程序在没有它的情况下运行良好。

我还尝试向 BlueToothCheckerController 添加演员表。也没用。

根据this 的回答,我还检查了我的构建阶段。如果重要的话,这就是它的样子。

有什么想法吗?

【问题讨论】:

  • BlueToothCheckerViewController 没有名为 setFromSegue: 的方法。
  • @HotLicks,我该如何/在哪里设置?

标签: ios objective-c


【解决方案1】:

我相信你的问题是这样的:

BlueToothCheckerView *btcv = [segue destinationViewController];

虽然您说目标控制器是BlueToothCheckerView,但错误消息说它实际上是BlueToothCheckerViewController

由于控制器没有属性,所以失败了。

【讨论】:

  • 在我编译代码之前,我得到了一个Property 'fromSegue' not found on object type BlueToothCheckerViewControllerfromSegue 是在BlueToothCheckerView 中创建的,xib 的自定义类也设置为那个。然后将其委托给 BlueToothCheckerViewController。
  • 我正在回答您在“问题:”下列出的错误。不过,一般来说,您必须在具有该属性的类型的对象上设置该属性。换句话说,如果你有一个BlueToothCheckerViewController ,它的视图是一个BlueToothCheckerView ,并且视图有这个属性,那么你需要在视图上设置它。
  • 啊,做到了。我将fromSegue 放入BlueToothCheckerViewController 并修改了任何调用它的内容。谢谢。
猜你喜欢
  • 2014-08-23
  • 1970-01-01
  • 2018-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多