UISegmentedControl setSelectedSegmentIndex:没有 valueChanged 操作
我正在通过代码设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的问题是一个明智的问题;我只能建议您发现了一个错误,因为这很不寻常。通常,更改代码中的控件不会导致发出任何控件事件。设置 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'scurrentPage
doesn't emit a Value Changed event. Setting a UISlider'svalue
doesn't emit a Value Changed event. Setting a UISwitch'son
doesn't emit a Value Changed event. Similarly, setting a UITextField'stext
doesn't emit an Editing Changed event. So, the fact that changing a UISegmentedControl'sselectedSegmentIndex
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.
我在 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.
根据 Apple,
selectedSegmentIndex
属性通常用于确定:您也可以根据您的要求将其更改为不同的值。
(参考)
As per Apple
The
selectedSegmentIndex
property is generally used to determine:You can also change it to different value depend upon your requirement.
(ref)