Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
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];
}
 
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.
 
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.

Book:
Sams Teach Yourself IOS 5 Application Development in 24hours

it's rubbish
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.