好的,这就是正在发生的事情。如果我点击我所拥有的5个段中的段“0”(最右边,因为ios中的segmentcontrol从右边算起0),第一次通过正确的段,即“0”。但是我第二次点击它,它认为我点击了第3段?!这很奇怪!有人可以帮我吗?这是我的代码:
if ([[_segmentControlTableFunctions.subviews objectAtIndex:2]isSelected] )
{
[[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:newSelectedTintColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];
[self removeAllTables];
[self displayTables:[Globals sharedInstance].StartUp.Tables];
self.tableManagement.scrollEnabled = YES;
}else if ([[_segmentControlTableFunctions.subviews objectAtIndex:3]isSelected] ){
[[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:newSelectedTintColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];
[self removeAllTables];
[self displayTables:[Globals sharedInstance].StartUp.Tables];
}else if ([[_segmentControlTableFunctions.subviews objectAtIndex:4]isSelected] ){
[[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:newSelectedTintColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];
[self removeAllTables];
[self displayTables:[Globals sharedInstance].StartUp.Tables];
self.tableManagement.scrollEnabled = YES;
}else if ([[_segmentControlTableFunctions.subviews objectAtIndex:1]isSelected] ){
self.currentTable = @"-1";
_firstTableForCombine = @"-1";
_nextTable = @"-1";
_splitTable = @"-1";
_lastsegmentIndex = -1;
_segmentOneClicked = false;
_segmentTwoClicked = false;
_segmentZeroClicked = false;
[self.SplitForm removeFromSuperview];
[self.roundFrameForSplit removeFromSuperview];
[self.moveOrderToA removeFromSuperview];
[self.moveOrderToB removeFromSuperview];
[[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:newSelectedTintColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];
wait((int*)1000);
[self removeAllTables];
[self displayTables:[Globals sharedInstance].StartUp.Tables];
_segmentControlTableFunctions.selectedSegmentIndex = UISegmentedControlNoSegment;
[NSTimer scheduledTimerWithTimeInterval: 0.2
target: self
selector: @selector (changeToOriginalColor)
userInfo: nil
repeats: NO];
self.tableManagement.scrollEnabled = YES;
}else if ([[_segmentControlTableFunctions.subviews objectAtIndex:0]isSelected] ){
[[_segmentControlTableFunctions.subviews objectAtIndex:0] setTintColor:originalColor];
if(_segmentControlTableFunctions.selectedSegmentIndex == 0 && _segmentZeroClicked)
{
[self Normal];
}else
{
[self magnify];
}
}
编辑代码:
//This handles all the background color of the segment control when the user clicks on
//one of the functions.
if (_segmentControlTableFunctions.selectedSegmentIndex == 2 )
{
[[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:newSelectedTintColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];
[self removeAllTables];
[self displayTables:[Globals sharedInstance].StartUp.Tables];
self.tableManagement.scrollEnabled = YES;
}else if (_segmentControlTableFunctions.selectedSegmentIndex == 1 ){
[[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:newSelectedTintColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];
[self removeAllTables];
[self displayTables:[Globals sharedInstance].StartUp.Tables];
}else if (_segmentControlTableFunctions.selectedSegmentIndex == 0 ){
[[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:newSelectedTintColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];
[self removeAllTables];
[self displayTables:[Globals sharedInstance].StartUp.Tables];
self.tableManagement.scrollEnabled = YES;
}else if (_segmentControlTableFunctions.selectedSegmentIndex == 3){
self.currentTable = @"-1";
_firstTableForCombine = @"-1";
_nextTable = @"-1";
_splitTable = @"-1";
_lastsegmentIndex = -1;
_segmentOneClicked = false;
_segmentTwoClicked = false;
_segmentZeroClicked = false;
[self.SplitForm removeFromSuperview];
[self.roundFrameForSplit removeFromSuperview];
[self.moveOrderToA removeFromSuperview];
[self.moveOrderToB removeFromSuperview];
[[_segmentControlTableFunctions.subviews objectAtIndex:4] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:1] setTintColor:newSelectedTintColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:2] setTintColor:originalColor];
[[_segmentControlTableFunctions.subviews objectAtIndex:3] setTintColor:originalColor];
wait((int*)1000);
[self removeAllTables];
[self displayTables:[Globals sharedInstance].StartUp.Tables];
_segmentControlTableFunctions.selectedSegmentIndex = UISegmentedControlNoSegment;
[NSTimer scheduledTimerWithTimeInterval: 0.2
target: self
selector: @selector (changeToOriginalColor)
userInfo: nil
repeats: NO];
self.tableManagement.scrollEnabled = YES;
}else if ([[_segmentControlTableFunctions.subviews objectAtIndex:0]isSelected] ){
[[_segmentControlTableFunctions.subviews objectAtIndex:0] setTintColor:originalColor];
if(_segmentControlTableFunctions.selectedSegmentIndex == 0 && _segmentZeroClicked)
{
[self Normal];
return;
}else
{
[self magnify];
return;
}
}
答案 0 :(得分:1)
子视图不是静态的。我怀疑当你选择一个标签时,视图层次结构会发生变化(添加光泽,移动东西或其他东西),索引不再是你怀疑的。有人评论说,首先选择所选的段索引。我强烈建议切换到使用选定的段索引,而不是基于子视图中的索引。从所选索引返回的值始终是正确的。
答案 1 :(得分:0)
tintColor属性仅用于更改整个控件的色调。如果要更改所选段的颜色与未选择的段,则应使用setBackgroundImage:forState:barMetrics:而不是。如果这是您想要的外观,您将需要创建一个彩色图像。它可以这样做(sc是分段控件的出口):
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *selectedImage = [UIImage imageNamed:@"GreenColor.png"];
UIImage *unselectedImage = [UIImage imageNamed:@"GrayColor.png"];
[self.sc setBackgroundImage:selectedImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.sc setBackgroundImage:unselectedImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
}
答案 2 :(得分:0)
对于那些想知道的人来说,这对我有用。 :)
for (int i = 0; i < self.segmentControlTableFunctions.subviews.count; i++)
{
if ([[self.segmentControlTableFunctions.subviews objectAtIndex:i]isSelected] )
{
[[self.segmentControlTableFunctions.subviews objectAtIndex:i] setTintColor:newSelectedTintColor];
}
else
{
[[self.segmentControlTableFunctions.subviews objectAtIndex:i] setTintColor:originalColor];
}
}