加载selectedindex时,UISegmentedControl会触发ValueChanged [英] UISegmentedControl fires ValueChanged when loading selectedindex
问题描述
我有一个 UISplitViewController
实用程序类型的应用程序,其中每个 UIViewController
处理各种计算。我有一个带有分段控件的弹出窗口,允许用户设置某些选项。我遇到的问题是保存和重新加载 UITextfields
的内容和选定的段索引,如果段控制索引已保存并且== 1则会触发valuechanged事件来自 viewdidload
,因为州设置为
I have a UISplitViewController
utility type app where each UIViewController
handles various calculations. I have a popover with segmented controls that allows the user to set certain options. The problem I have is when saving and reloading the contents of UITextfields
and the selected segment index, if the segment control index was saved and is == 1 a valuechanged event is fired off from viewdidload
as the state is set from
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
theDepthSegment.selectedSegmentIndex = [[defaults objectForKey:@"updateMe"] intValue];
无论如何都要阻止valuechanged被触发,因为在视图中设置了selectedsegment吗?或者可能在某种程度上确定触摸事件与代码之间的区别
Is there anyway to prevent valuechanged being fired as the selectedsegment is set in view did load? or maybe someway to determine the difference between touch events versus code
这种情况似乎很奇怪,也许有另一种方式....
It seems strange this happens, maybe there is another way....
谢谢
推荐答案
考虑到视图只是加载,分段控件没有以前的价值,所以它会假设它确实被改变并采取相应的行动。您可能解决此问题的一种方法是在链接到分段控件的 IBAction
方法中设置 BOOL
值。在 viewDidLoad
方法的开头,比如说 wasLoaded = NO
(其中 wasLoaded
需要在头文件中声明)。然后你将分配你的 selectedSegmentIndex
。然后,声明 wasLoaded = YES
。最后,在您的 IBAction
方法中,例如
Considering the view is just loading, the segmented control doesn't have a previous value, so it will assume it is indeed being changed and acts accordingly. One way you could potentially get around this is to set up a BOOL
value in your IBAction
method linked to the segmented control. At the beginning of your viewDidLoad
method, say wasLoaded = NO
(where wasLoaded
will need to be declared in your header file). Then you'll assign your selectedSegmentIndex
. Afterwards, declare wasLoaded = YES
. Finally, in your IBAction
method, say something like
if (wasLoaded) {
// do what you need to do since this isn't when it's being initialized
}
这篇关于加载selectedindex时,UISegmentedControl会触发ValueChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!