***由于未捕获的异常'NSInvalidArgumentException'而终止应用

时间:2015-07-04 05:21:31

标签: objective-c exception error-handling

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

1 个答案:

答案 0 :(得分:0)

显然,您正在向View发送事件而不是ViewController,请替换此

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

用这个

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

它应该有效