问题描述
我正在通过代码设置UISegmentedControl的selectedSegmentIndex。
每当我执行此操作时,将调用valueChanged操作。这对我来说听起来合乎逻辑,但是有没有办法在不调用动作的情况下设置所选的段?它应该只是更新显示。
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.
我不止一次使用UISegmentedControl,直到现在我甚至没有注意到这种行为。但是这次我需要在选择特殊段时发出警报。因此,如果视图出现并且我想显示先前选择的值,则无法使用调用的操作。
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.
我可以断开操作,更改selectedValue并重新连接操作。但也许有更好的方法。
I could disconnect the action, change the selectedValue and reconnect the action. But maybe there is a better way.
推荐答案
你的问题是明智的;我只能建议您发现了一个错误,因为这是非常不寻常的。通常,更改代码中的控件不会导致发出任何控件事件。设置UIDatePicker的日期
不会发出Value Changed事件。设置UIPageControl的 currentPage
不会发出Value Changed事件。设置UISlider的值
不会发出Value Changed事件。在上设置UISwitch的不会发出Value Changed事件。同样,设置UITextField的
文本
不会发出编辑更改事件。因此,更改UISegmentedControl的 selectedSegmentIndex
发出Value Changed事件的事实感觉不对。我要提交一个错误,我认为你也应该这样做。
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.
我没有看到一种明显的方法来确定用户是否触发了Value Changed事件点击或以编程方式。你必须提高和降低BOOL旗帜等。
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.
编辑:这在iOS 5中已得到修复。
This is fixed in iOS 5.
这篇关于UISegmentedControl setSelectedSegmentIndex:不带valueChanged Action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!