UISegmentedControl 在 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];
当我单步执行程序时 [bottomSegmentedControl setSelectedSegmentIndex:0];
不会触发 UIControlEventValueChanged 事件,这是我在 segmentedControlChanged:
中执行一些操作的
地方适用于 iOS 4.3,但不适用于 iOS5。我该如何解决这个问题?
I have a 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];
When I step through the program [bottomSegmentedControl setSelectedSegmentIndex:0];
does not trigger a UIControlEventValueChanged event which is where I do some stuff in segmentedControlChanged:
This used to work in iOS 4.3 but not in iOS5. How can I resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题已在这里得到解答: https://stackoverflow.com/a/8054774/883413
在 iOS5 中,来自 a 的事件如果以编程方式更改值,则不再触发 UISegmentedControl,以与其他 UIControl 的行为保持一致。
一个快速的解决方案是添加:
之后
,它会
在 iOS<5 中调用您的选择器两次
This question has been answered here: https://stackoverflow.com/a/8054774/883413
In iOS5 the events from a UISegmentedControl are no longer fired if the value is changed programatically, to keep consistency with the behaviour of other UIControls.
A quick solution is to add:
after
however it will call your selector
twice in iOS<5
这似乎是一个已知问题。目前尚不清楚苹果是否认为新行为是一项功能还是一个错误。
This seems to be a known issue. It's not clear whether Apple considers the new behavior to be a feature or a bug.