简体   繁体   English

我认为我在分配segmentcontrol时犯了一些错误。我遇到了一个错误[无法识别的选择器发送到实例]

[英]I think i have done some mistake in allocating segmentcontrol..i am getting an error [unrecognized selector sent to instance]

    Segcontrol = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Mind", @"Munches", nil]];
    Segcontrol.frame=CGRectMake(CGRectGetWidth(self.view.frame)/4.5, self.view.frame.size.height/4, CGRectGetWidth(self.view.frame)/1.8, CGRectGetHeight(self.view.frame)/12);
    [Segcontrol addTarget:Segcontrol action:@selector(segtap:) forControlEvents:UIControlEventValueChanged];
    [Segcontrol setSelectedSegmentIndex:0];
    [Segcontrol setTintColor:[UIColor blueColor]];
    Segcontrol.layer.cornerRadius=5;
    [self.view addSubview:Segcontrol];


-(void)segtap:(id)sender{
    if (Segcontrol.selectedSegmentIndex==0) {

        self.view.backgroundColor=[UIColor blueColor];


    }else if (Segcontrol.selectedSegmentIndex==1){

        self.view.backgroundColor=[UIColor redColor];


    }

}

You are adding the SegmentControl object Segcontrol as target to the segment action. 您正在将SegmentControl对象Segcontrol作为细分操作的目标。 Rather than this the object self should be added as target. 与此相反,应将对象self添加为目标。 Change the code like below and you are good to go. 像下面这样更改代码,您就可以开始了。

[Segcontrol addTarget:self action:@selector(segtap:) forControlEvents:UIControlEventValueChanged];

Implement segtap method like this. 像这样实现segtap方法。

 -(void)segtap:(id)sender
 {
     switch (Segcontrol.selectedSegmentIndex)
     {
     case 0:
         NSLog(@"Index 0 selected");
         break;
     case 1:
        NSLog(@"Index 1 selected");
        break;
     }
 }

I think you might be forgotten to pass argument to the method. 我认为您可能会忘记将参数传递给该方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到一个“无法识别的选择器发送到实例”错误? - Why am I getting a `unrecognized selector sent to instance` error? 为什么我收到CIFilter的“无法识别的选择器发送给类”错误? - Why am I getting an “unrecognized selector sent to class” error for CIFilter? 定义方法后,为什么会收到“无法识别的选择器发送到实例”错误? - Why am I getting an “unrecognised selector sent to instance” error when I have defined the method? 为什么我的MapViewController收到无法识别的选择器发送到实例异常? (iOS) - Why am I getting a unrecognized selector sent to instance exception with my MapViewController? (iOS) Swift 无法识别的选择器发送到实例 - 我错过了什么? - Swift unrecognized selector sent to instance - What am I missing? 为什么我会收到发送到 class 的无法识别的选择器? - Why am I getting unrecognized selector sent to class? 为什么我会收到“ [__NSArrayI allKeys]:无法识别的选择器发送到实例” /为什么要进行NSDictionary转换? - Why am I getting “[__NSArrayI allKeys]: unrecognized selector sent to instance” / why is NSDictionary transforming? 如何解决错误“无法识别的选择器已发送至实例…”? - How can I fix the error “ unrecognized selector sent to instance…”? 如何调试“无法识别的选择器发送到实例”错误 - How can I debug 'unrecognized selector sent to instance' error 选择器语法:为什么我得到无法识别的选择器错误? - selector syntax: why I am getting unrecognized selector error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM