[英]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];
}
}
您正在将SegmentControl
对象Segcontrol
作为细分操作的目标。 与此相反,应将对象self
添加为目标。 像下面这样更改代码,您就可以开始了。
[Segcontrol addTarget:self action:@selector(segtap:) forControlEvents:UIControlEventValueChanged];
像这样实现segtap方法。
-(void)segtap:(id)sender
{
switch (Segcontrol.selectedSegmentIndex)
{
case 0:
NSLog(@"Index 0 selected");
break;
case 1:
NSLog(@"Index 1 selected");
break;
}
}
我认为您可能会忘记将参数传递给该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.