UISegmentedControl 在 iOS5 中未检测到段更改

发布于 2024-12-14 06:59:12 字数 1498 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╰沐子 2024-12-21 06:59:12

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

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

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

[bottomSegmentedControl sendActionsForControlEvents:UIControlEventValueChanged];

之后

[bottomSegmentedControl setSelectedSegmentIndex:0];

,它会

segmentedControlChanged:

在 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:

[bottomSegmentedControl sendActionsForControlEvents:UIControlEventValueChanged];

after

[bottomSegmentedControl setSelectedSegmentIndex:0];

however it will call your selector

segmentedControlChanged:

twice in iOS<5

小帐篷 2024-12-21 06:59:12

这似乎是一个已知问题。目前尚不清楚苹果是否认为新行为是一项功能还是一个错误。

This seems to be a known issue. It's not clear whether Apple considers the new behavior to be a feature or a bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文