【问题标题】:*** Terminating app due to uncaught exception 'NSInvalidArgumentException'*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序
【发布时间】:2015-09-21 21:49:08
【问题描述】:

2015-07-03 22:13:37.230 Hyponerd[64413:2308636] 加载了它的视图 2015-07-03 22:13:39.860 Hyponerd[64413:2308636]-[BNRHyposisView MySegmentedControlAction:]:无法识别的选择器发送到实例 0x7fcbd27623b0 2015-07-03 22:13:39.866 Hyponerd[64413:2308636] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[BNRHyposisView MySegmentedControlAction:]:无法识别的选择器发送到实例 0x7fcbd27623b0” * 首先抛出调用栈:

这是什么意思?我一直在尝试解决这四个小时 我的代码是:

    @implementation BNRHyponosisViewController

    -(void)loadView {
        //Create a view
        CGRect frame = [UIScreen mainScreen].bounds;
        _backgroundView = [[BNRHyposisView alloc]initWithFrame:frame];

    NSArray *colors = @[@"Red", @"Blue", @"Green"];

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:colors];

    segmentedControl.frame = CGRectMake(40, 200, 250, 50);
    segmentedControl.backgroundColor = [UIColor blackColor];
    segmentedControl.selectedSegmentIndex = 1;
    [_backgroundView addSubview:segmentedControl];

    [segmentedControl addTarget:_backgroundView    action:@selector(MySegmentedControlAction:) forControlEvents:UIControlEventValueChanged];

    self.view = _backgroundView;

}
          -(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:  (NSBundle *)nibBundleOrNil {
              self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

       if (self) {
            self.tabBarItem.title = @"Hypnotize";

        UIImage *image = [UIImage imageNamed:@"Hypno.png"];

        self.tabBarItem.image = image;

    }   return self;
}
    -(void)viewDidLoad {
         [super viewDidLoad];
         NSLog(@"loaded its view");

}
    -(void)MySegmentedControlAction:(UISegmentedControl*)segment
{
        if (segment.selectedSegmentIndex == 0) {
        [_backgroundView setCircleColor:[UIColor redColor]];
        } else if (segment.selectedSegmentIndex == 1) {
        [_backgroundView setCircleColor:[UIColor greenColor]];
    } else if (segment.selectedSegmentIndex == 2) {
        [_backgroundView setCircleColor:[UIColor blueColor]];
    }

}

@end

【问题讨论】:

    标签: objective-c exception error-handling


    【解决方案1】:

    显然,您将事件发送到 View 而不是您的 ViewController,替换它

    [segmentedControl addTarget:_backgroundView action:@selector(MySegmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    

    有了这个

    [segmentedControl addTarget:self action:@selector(MySegmentedControlAction:) forControlEvents:UIControlEventValueChanged];
    

    它应该可以工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多