Note that there are some explanatory texts on larger screens.

plurals
  1. POUISegmentedControl: Programmatically Set Index?
    text
    copied!<p>I'm trying to load a user setting into a segmented control. The NSUserDefault reads correctly, and the preference is correctly translated into an index. However, the segmented control does not seem to respond to setting the index, preferring to stay at index 0.</p> <p>The code clean compiles with <code>-Wall -Wextra</code>, and <code>clang</code> does not report any issues. I've also run with both Leaks and Zombies - OK. And the <code>ASSERTs</code> below do not fire.</p> <p>The UISegmentedControl were created using Interface Builder (there are 4 total on the view). I've verified the connections. I've tried calling <code>-loadPreferences</code> in <code>-viewDidLoad</code>, <code>-viewWillAppear</code>, and <code>-viewDidAppear</code>.</p> <p>Are there any tricks I'm missing? Should I be calling a <code>needsUpdate</code> or some other method to get the controls to repaint?</p> <p>Jeff</p> <pre><code>-(void)loadPreferences { NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; ... int days = [userDefaults integerForKey:kDirtyAfter]; ASSERT(IsValidDirtyDays((DirtyDays)days)); if(!IsValidDirtyDays((DirtyDays)days)) days = (int)DirtyDaysDefault; int idx; switch(days) { case DirtyDays1: idx = 0; break; case DirtyDays3: idx = 1; break; case DirtyDays7: idx = 2; break; case DirtyDays14: idx = 3; break; case DirtyDays28: idx = 4; break; default: idx = 1; } // dirtyAfterSegment.selectedSegmentIndex = idx; [dirtyAfterSegment setSelectedSegmentIndex:idx]; ASSERT(dirtyAfterSegment.selectedSegmentIndex == idx); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload