UISegmentedControl 更改事件未在 iOS5 中触发 [英] UISegmentedControl change event not firing in iOS5

查看:14
本文介绍了UISegmentedControl 更改事件未在 iOS5 中触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UISegmentedControl,它的Value changed"事件在 Interface Builder 中被连接起来,以调用我的控制器的 -(IBAction)segmentChangeAction:(id)sender;

I have a UISegmentedControl whose "Value changed" event is wired up in Interface Builder to call my controller's -(IBAction)segmentChangeAction:(id)sender;

当用户点击控件以更改选定的段时,无论在 iOS4 还是 iOS5 中,都会按预期调用 segmentChangeAction.

When the user taps on the control to change the selected segment, as expected segmentChangeAction is called whether in iOS4 or iOS5.

当我通过 segmentedControl.selectedSegmentIndex = newIndex; 以编程方式更改所选段时,在 iOS4 上调用 segmentChangeAction 并且该段反映了新的选择.然而在 iOS5 segmentChangeActionnot 被调用,但段确实反映了新的选择.

When I programmatically change the selected segment through segmentedControl.selectedSegmentIndex = newIndex;, on iOS4 segmentChangeAction is called and the segment reflects the new selection. However on iOS5 segmentChangeAction is not called, yet the segment does reflect the new selection.

这是 iOS5 的变化吗?当我以编程方式更改选择时,我可以做些什么来在 iOS5 上调用 segmentChangeAction?

Is this a change in iOS5? Is there anything I can do to get segmentChangeAction called on iOS5 when I programmatically change the selection?

推荐答案

这是 iOS 5 中的一个更改,以便 UISegmentedControl 与所有其他控件保持一致.

This is a change in iOS 5 in order for UISegmentedControl to be consistent with all other controls.

这个想法是动作应该只作为用户交互的结果自动触发.在 iOS 5 之前,UISegmentedControl 的操作会因为用户交互 编程交互而被触发.但是,以编程方式启动更改意味着您也可以自己执行 [myControl sendActionsForControlEvents:UIControlEventValueChanged].

The idea is that the action should only fired automatically as a result of user interaction. Prior to iOS 5, UISegmentedControl's actions would be fired because of user interaction and programmatic interaction. However, initiating the change programmatically means that you can also do [myControl sendActionsForControlEvents:UIControlEventValueChanged] yourself.

但是,您必须小心这一点.说你这样做:

However, you have to be careful with this. Say you do:

[segmentedControl setSelectedSegmentIndex:newIndex];
[segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];

如果您在 iOS 5 上构建并运行它,它会按预期工作.如果您在 iOS 4 上构建并运行它,您的操作会触发两次(一次是在您 setSelectedSegmentIndex 时,另一次是在您 sendActions...代码>).

If you build and run this on iOS 5, it works as you expect. If you build and run this on iOS 4, you'll get your actions fired twice (once when you setSelectedSegmentIndex and again when you sendActions...).

解决这个问题的方法是进行某种保护.这可能是运行时检查,表明您在 iOS 5+ 设备上运行,或者甚至可能是更普通的东西,如下所示:

The way around this is to do some sort of guard. This could be a runtime check to indicate that you're running on an iOS 5+ device, or could even be something more mundane, like this:

// changingIndex is a BOOL ivar
changingIndex = YES;
[segmentedControl setSelectedSegmentIndex:newIndex];
changingIndex = NO;
[segmentedControl sendActionsForControlEvents:UIControlEventValueChanged];

然后在您的操作方法中...

and then in your action method...

- (void)segmentedControlSelectedIndexChanged:(id)sender {
  if (!changingIndex) {
    // your action code here, guaranteed to only run as a result of the sendActions... msg
  }
}

这篇关于UISegmentedControl 更改事件未在 iOS5 中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆