我有一个UISegmentedControl
UIBarButtonItem *flex = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]autorelease];
bottomSegmentedControl = [[UISegmentedControl alloc] initWithItems:nil];
[bottomSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"messages.png"] atIndex:0 animated:YES];
[bottomSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"news.png"] atIndex:1 animated:YES];
[bottomSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"chart.png"] atIndex:2 animated:YES];
//bottomSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Messages",@"News",@"Chart", nil]];
//bottomSegmentedControl.tintColor = [UIColor blackColor];
[bottomSegmentedControl addTarget:self action:@selector(segmentedControlChanged:)forControlEvents:UIControlEventValueChanged];
bottomSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
bottomSegmentedControl.frame = CGRectMake(0,0,300,30);
bottomSegmentedControl.momentary = NO;
[bottomSegmentedControl setSelectedSegmentIndex:0];
UIBarButtonItem *segButton = [[UIBarButtonItem alloc] initWithCustomView:bottomSegmentedControl];
当我单步执行程序[bottomSegmentedControl setSelectedSegmentIndex:0];
时,不会触发UIControlEventValueChanged事件,这是我在segmentedControlChanged:
这曾经在iOS 4.3中运行,但在iOS5中运行。我该如何解决这个问题?
答案 0 :(得分:5)
此问题已在此处得到解答:https://stackoverflow.com/a/8054774/883413
在iOS5中,如果以编程方式更改值,则不再触发UISegmentedControl中的事件,以保持与其他UIControl的行为的一致性。
快速解决方案是添加:
[bottomSegmentedControl sendActionsForControlEvents:UIControlEventValueChanged];
后
[bottomSegmentedControl setSelectedSegmentIndex:0];
然而它会调用你的选择器
segmentedControlChanged:
在iOS< 5 中两次
答案 1 :(得分:1)
这似乎是known issue。目前尚不清楚Apple是否将新行为视为特征或错误。