[英]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.