问题描述
|
我已经使用以下代码创建了分段控制器,但是我无法区分哪个被选中和哪个未被选中。我该如何区分?
UISegmentedControl *segmentedControl;
segmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[segmentedControl insertSegmentWithTitle:@\"Male\" atIndex:0 animated:YES];
[segmentedControl insertSegmentWithTitle:@\"Female\" atIndex:1 animated:YES];
segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;
segmentedControl.frame = CGRectMake(100,10,200,30);
[segmentedControl setMomentary:YES];
[segmentedControl addTarget:self action:@selector(segmentSwitch:) forControlEvents:UIControlEventValueChanged];
- (void)segmentSwitch:(id)sender
{
segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
UIView *firstView =[[UIView alloc]initWithFrame:CGRectMake(0,20,20)];
firstView.backgroundColor=[UIColor greenColor];
UIView *secondView =[[UIView alloc]initWithFrame:CGRectMake(0,20)];
firstView.backgroundColor=[UIColor brownColor];
if (selectedSegment == 0)
{
NSLog(@\"first segment\");
//toggle the correct view to be visible
strGender =[[NSMutableString alloc]initWithString:@\"Male\"];
[firstView setHidden:NO];
[secondView setHidden:YES];
}
else
{
NSLog(@\"second segment\");
//toggle the correct view to be visible
strGender =[[NSMutableString alloc]initWithString:@\"Female\"];
[firstView setHidden:YES];
[secondView setHidden:NO];
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)