UISegmentedControl setSelectedSegmentIndex:没有 valueChanged 操作

发布于 2024-10-09 13:39:52 字数 301 浏览 0 评论 0原文

我正在通过代码设置 UISegmentedControl 的 selectedSegmentIndex。
每当我这样做时,就会调用 valueChanged 操作。这对我来说听起来很合乎逻辑,但是有没有办法在不调用操作的情况下设置选定的段?它应该只是更新显示。

我不止一次使用过 UISegmentedControl,直到现在我什至没有注意到这种行为。但这次我需要在选择特殊段时发出警报。因此,如果视图出现并且我想显示以前选择的值,我将无法忍受调用的操作。

我可以断开该操作,更改 selectedValue 并重新连接该操作。但也许有更好的方法。

I'm setting the selectedSegmentIndex of an UISegmentedControl via code.
Whenever I do this the valueChanged action gets called. This sounds logical for me, but is there a way to set the selected segment without invoking the action? It should just update the display.

I've used UISegmentedControl more than once, and until now I didn't even noticed that behavior. But this time I need to present an alert if a special segment is selected. So I can't live with the invoked action if the view appears and I want to show the previous selected value.

I could disconnect the action, change the selectedValue and reconnect the action. But maybe there is a better way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

仙气飘飘 2024-10-16 13:39:52

你的问题是一个明智的问题;我只能建议您发现了一个错误,因为这很不寻常。通常,更改代码中的控件不会导致发出任何控件事件。设置 UIDatePicker 的 date 不会发出 Value Changed 事件。设置 UIPageControl 的 currentPage 不会发出 Value Changed 事件。设置 UISlider 的 value 不会发出 Value Changed 事件。设置 UISwitch 的 on 不会发出 Value Changed 事件。同样,设置 UITextField 的 text 不会发出 Editing Changed 事件。因此,更改 UISegmentedControl 的 selectedSegmentIndex 会发出 Value Changed 事件这一事实感觉是错误的。我将提交一个错误,我想你也应该这样做。

我没有看到明显的方法来确定值更改事件是由用户点击还是以编程方式触发。你必须升高和降低一个 BOOL 标志或其他东西。

编辑:这在 iOS 5 中已修复。

Your question is a sensible one; I can only suggest that you've found a bug, since this is so unusual. Usually, changing a control in code doesn't cause any control events to be emitted. Setting a UIDatePicker's date doesn't emit a Value Changed event. Setting a UIPageControl's currentPage doesn't emit a Value Changed event. Setting a UISlider's value doesn't emit a Value Changed event. Setting a UISwitch's on doesn't emit a Value Changed event. Similarly, setting a UITextField's text doesn't emit an Editing Changed event. So, the fact that changing a UISegmentedControl's selectedSegmentIndex emits a Value Changed event feels wrong. I'm going to file a bug and I think you should too.

I don't see an obvious way to determine whether the Value Changed event was triggered by the user tapping or programmatically. You'll have to raise and lower a BOOL flag or something.

EDIT: This is fixed in iOS 5.

归属感 2024-10-16 13:39:52

我在 iOS 7.1 中仍然遇到这个问题。

我正在重新加载表并设置段索引,但它正在调用“值已更改”委托。
为了避免这种情况,我首先取消了“段控件”委托,然后以编程方式设置值,并再次将委托重置为当前类。这不会调用“值更改”委托。

I still have this issue in iOS 7.1.

I was reloading table and setting segment index but it was calling 'value changed' delegate.
To avoid that I nilled 'segment control's' delegate first and then set value programatically, and again reset the delegate to current class. This won't call 'value change' delegate.

[旋木] 2024-10-16 13:39:52

根据 Apple,

selectedSegmentIndex 属性通常用于确定:

用于标识所选段(即最后触及的段)的索引号。

默认值为UISegmentedControlNoSegment(未选择任何分段),直到用户触摸分段。将此属性设置为 -1 以关闭当前选择。

您也可以根据您的要求将其更改为不同的值。

当控件处于瞬时模式时,

UISegmentedControl 将忽略此属性。当用户触摸某个段以更改选择时,会生成控件事件UIControlEventValueChanged;如果分段控件设置为响应此控件事件,它将向其目标发送一条操作消息。

参考

As per Apple

The selectedSegmentIndex property is generally used to determine:

the index number for identifying the selected segment (that is, the last segment touched).

The default value is UISegmentedControlNoSegment (no segment selected) until the user touches a segment. Set this property to -1 to turn off the current selection.

You can also change it to different value depend upon your requirement.

UISegmentedControl ignores this property when the control is in momentary mode. When the user touches a segment to change the selection, the control event UIControlEventValueChanged is generated; if the segmented control is set up to respond to this control event, it sends a action message to its target.

(ref)

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