title: How do I initialize a segmented control with a value from NSUserDefaults
body: <p>My <code>UISegmentedControl</code> will not stay selected. I have made sure that <a href="https://stackoverflow.com/questions/19793576/ios-7-uisegmentedcontrol-not-staying-highlighted">momentary</a> is NO. So the solutions I have come across on here have not helped. </p>
  
  <p>Would someone please be able to point me in the right direction?</p>
  
  <p>EDIT</p>
  
  <p>Thought I might make this question a bit clearer.</p>
  
  <p>I have a <code>UISegmentedControl</code> and it has four selections (10,20,30,40) which changes the amount of questions asked on my quiz page. Making a selection works fine and changes the amount of questions. </p>
  
  <p>But when I leave that view and go back later on to change the amount of questions again, it shows the selected as 10 even if I have selected something else. </p>
  
  <p>How can I keep it showing the actual selected value?</p>
  
  <p>EDIT </p>
  
  <p>The number of questions is saved in NSUserDefaults.</p>
  
  <pre><code>[[NSUserDefaults standardUserDefaults] setInteger:amountOfQuestions forKey:@"Amount"]
  </code></pre>
  
  <p>How do I initialize a segmented control with a value from NSUserDefaults?</p>
  
  <p>EDIT - Solved </p>
  
  <pre><code>#SettingsViewController .m file.
  - (void)viewDidLoad
  {
  amountOfQuestions = [[NSUserDefaults standardUserDefaults] integerForKey:@"Amount"];
  if (amountOfQuestions == 10) {
  mySegment.selectedSegmentIndex = 0;
  }
  </code></pre>
  
  <p>I did not have the below code in my <code>IBAction</code> for my segmented control. So when i tried the above code it did not work. Now it works a treat.</p>
  
  <pre><code>[[NSUserDefaults standardUserDefaults]synchronize]; 
  </code></pre>