NSUserDefaults and Modal segues

Discussion in 'iOS Programming' started by nashyo, Feb 1, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    I'm trying to learn about data persistence from a book, and I'm using Xcode 4.2 to practice. I'm confused about something and I'm hoping someone out there can help me understand.

    In IOS 5, using Xcode 4.2; on an iPhone storyboard, does a modal segue deallocate the view I'm leaving?

    If so, why doesn't NSUserDefaults assign the text label in my UIButtons on previous view? I'm forcing NSUserDefaults to synchronise. The UIButton text does eventually populate, when i exist and re-open the application.

    Code:
    - (void) saveCardiacString:(NSString *) myString
    {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [[NSUserDefaults standardUserDefaults] setObject:myString forKey:@"Cardiac"];
        [userDefaults synchronize];
    }
    Code:
    - (void) saveRespiratoryString:(NSString *) myString
    {
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        [[NSUserDefaults standardUserDefaults] setObject:myString forKey:@"Respiratory"];
        [userDefaults synchronize];
    }

    Code:
    - (void)pickerView:(UIPickerView *)pickerView 
          didSelectRow:(NSInteger)row 
           inComponent:(NSInteger)component
    {
        if ([[_currentOptions objectAtIndex:0] isEqualToString:@"no cardiac failure"]) {
        [self saveCardiacString:[_currentOptions objectAtIndex:row]];
        } else if ([[_currentOptions objectAtIndex:0] isEqualToString:@"no dyspnoea"]) {
        [self saveRespiratoryString:[_currentOptions objectAtIndex:row]];
        }
        NSLog(@"Cardiac string is set as %@", [self retrieveCardiacStringValue]);
        NSLog(@"Respiratory string is set as %@", [self retrieveRespiratoryStringValue]);
    }
    NSLogs show that NSUserDefaults are assigned.

    Previous View

    Code:
    - (NSString *) retrieveCardiacStringValue
    {
        return [[NSUserDefaults standardUserDefaults] objectForKey:@"Cardiac"];
    }
    Code:
    - (NSString *) retrieveRespiratoryStringValue
    {
        return [[NSUserDefaults standardUserDefaults] objectForKey:@"Respiratory"];
    }
    the following generate*options methods call for an NSArray to be generated, which is then passed to the picker in the modal view when a button is pressed.

    Code:
    - (void)viewDidLoad
    {
        _currentCardiacOptions = [Cardiac generateCardiacOptions];
        _currentRespiratoryOptions = [Respiratory generateRespiratoryOptions];
        
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Cardiac"] == nil) {
            [_cardiacValueBtn setTitle:@"0" forState:UIControlStateNormal];
        } else {
            [_cardiacValueBtn setTitle:[self retrieveCardiacStringValue] forState:UIControlStateNormal];
        }
        
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Respiratory"] == nil) {
            [_respriatoryValueBtn setTitle:@"0" forState:UIControlStateNormal];
        } else {
            [_respriatoryValueBtn setTitle:[self retrieveRespiratoryStringValue] forState:UIControlStateNormal];
        }
        
        [super viewDidLoad];
    }
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Might I suggest starting a new project and turn off the storyboarding, so that you can concentrate on the data persistence topics being taught from the book? Once you are comfortable with them, you could go back and revisit how storyboards would affect their implementation.

    Also, as a courtesy to others, when you refer to a book, please provide the title, author, and edition, at minimum.
     
  3. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    Book:
    Sams Teach Yourself IOS 5 Application Development in 24hours

    it's rubbish
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    I've heard that about that book.

    Time for a new book perhaps?
     
  5. ppilone macrumors 6502

    Joined:
    Jan 20, 2008
    #5
    In addition to concentrating on data persistence you might want to make sure you understand the lifecycle of a view controller and its view. Specifically the differences between viewDidLoad:, viewWill/DidAppear:, and viewWill/DidDisappear:.

    I think the combination of two will get you to see why the behavior of your code isn't as expected.
     
  6. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol

Share This Page