且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

加载selectedindex时,UISegmentedControl会触发ValueChanged

更新时间:2023-12-03 13:50:46

考虑到视图只是加载,分段控件没有以前的价值,所以它会假设它确实被改变并采取相应的行动。您可能解决此问题的一种方法是在链接到分段控件的 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 
}