Change segmented selected index based off NSUserDefaults

Discussion in 'iOS Programming' started by newtoiphonesdk, Feb 11, 2012.

  1. macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I am using a segmented control in my settings view to make certain changes. Everything is working fine for when I change values on the control, but how do I get it to retain the index based off of the NSUserDefault?
     
  2. macrumors regular

    Joined:
    Aug 28, 2008
    #2
    Not sure I understand the question.
    NSUserDefaults is a dictionary and does not like the zero form an integer (segmentIndex). If you make your int into an NSString it can be stored in the Dictionary with out problems even when zero. Get it back with intValue.

    - Olaf
     
  3. thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    Finally got it working. Problem I had originally was I set the int in wrong order for segmentedcontrol. Here is what I ended with:
    Code:
    -(void)viewWillAppear:(BOOL)animated {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSURL *sermons = [defaults URLForKey:@"sermon"];
        NSString *absolutely = sermons.absoluteString;
        NSLog(@"%@", absolutely);
        if ([absolutely isEqualToString:@"http://www.bellavenue.org/sermons.htm"]) {
            segment.selectedSegmentIndex = 1;
        }
        if ([absolutely isEqualToString:@"http://www.316apps.com/iphonesermons.html"]) {
            segment.selectedSegmentIndex = 0;
        }
    
    }
    
     
  4. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    I would recommend using NSNumber rather than NSString to store ints in NSUserDefaults.
     
  5. macrumors regular

    Joined:
    Aug 28, 2008
    #5
    Ah, yes, true. NSNumber would keep it in the family.

    - Olaf
     
  6. thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #6
    I didn't think this was storing any int, just the URL of websites.
     

Share This Page