UISegmentedControl change event not firing in iOS5(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 segmentChangeAction
上 not 被调用,但段确实反映了新的选择.
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 中触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UISegmentedControl 更改事件未在 iOS5 中触发
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01