Question

I have an if statement that's condition is passed to this implementation file via NSUserDefaults as seen below.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *code = [defaults objectForKey:@"codeKey"];
selectedCodeLocal = code; 

After this code to retrieve the string variable, I have an if statement:

if (selectedCodeLocal == @"1")
    textView.text = "@blah blah blah";
else
    textview.text = "@abcdefghijklmnop";

When I build and run, it appears that the variable IS being passed, but it's not being passed until AFTER the if statement executes.

I have places NSLog's around this code that return my selectedCodeLocal string variable and the variable's value is always one step behind. (For instance if I first pass it as 4, then pass it as 1, it will be returned in the log first as 1, then as 4, then as 1) Sorry if I've confused you with that.

UPDATE:

- (void)viewDidLoad {

[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize];
selectedCodeLocal = [defaults objectForKey:@"codeKey"];
NSLog(@"set: %@",selectedCodeLocal);

self.navigationItem.title = selectedCodeLocal;

[textView setClipsToBounds:NO];
[textView setEditable:NO];
[textView setFrame:CGRectMake(20, 100, 50, 50)];

if ([selectedCodeLocal isEqualToString:@"100"])
    textView.text = @"abc";
else
    textView.text = @"xyz";

}

The NSLog still displays the old value of selectedCodeLocal.

UPDATE: Here's where that Key is set. (in the previous View)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

//Get the selected code

NSString *selectedCode = nil;

if(searching)
    selectedCode = [copyListOfItems objectAtIndex:indexPath.row];
else {

    NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"codesKey"];
    selectedCode = [array objectAtIndex:indexPath.row];
}

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:selectedCode forKey:@"codeKey"];
[defaults synchronize];

}

@Firoze Lafeer: Does this answer your question? storyboard

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top