UISegmentedControl不检测iOS5中的段更改

时间:2011-11-04 19:33:05

标签: iphone objective-c ios5 uibarbuttonitem uisegmentedcontrol

我有一个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中运行。我该如何解决这个问题?

2 个答案:

答案 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是否将新行为视为特征或错误。