UISegmentedControl不检测iOS5中的段更改

She*_*lam 3 iphone objective-c uisegmentedcontrol uibarbuttonitem ios5

我有一个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];
Run Code Online (Sandbox Code Playgroud)

当我单步执行程序时[bottomSegmentedControl setSelectedSegmentIndex:0];不会触发UIControlEventValueChanged事件,这是我在做某些事情的地方segmentedControlChanged:

这曾经在iOS 4.3中工作,但在iOS5中没有.我该如何解决这个问题?

Ric*_*tos 5

这个问题已在这里得到解答:https://stackoverflow.com/a/8054774/883413

在iOS5中,如果以编程方式更改值,则不再触发UISegmentedControl中的事件,以保持与其他UIControl的行为的一致性.

一个快速的解决方案是添加:

[bottomSegmentedControl sendActionsForControlEvents:UIControlEventValueChanged];
Run Code Online (Sandbox Code Playgroud)

[bottomSegmentedControl setSelectedSegmentIndex:0];
Run Code Online (Sandbox Code Playgroud)

但它会调用你的选择器

segmentedControlChanged:
Run Code Online (Sandbox Code Playgroud)

在iOS <5中两次