Reputation: 690
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?
Upvotes: 0
Views: 501
Reputation: 17143
With a storyboard, and a segue attached to a tableview cell, your tableView:didSelectRowAtIndexPath
will be called after the new view controller is loaded and pushed.
So in other words, you are setting this key in your user's defaults after you have already read the value.
The right time to set up any data you need for the detail view controller is in the prepareForSegue:sender:
method on your tableview controller. tableView:didSelectRowAtIndexPath:
is too late if you are using a segue on a tableview in a storyboard.
Other thoughts:
Everyone else is right that you should be using isEqualToString:
, not ==
. The fact that the latter is working for you is really an accident of implementation. You need to do the right thing and not depend on that. Using '==' (which is pointer comparison in this case) is wrong.
Speaking of doing the right thing, you should consider if selectedCode
really belongs in your user's preferences (NSUserDefaults). It would be much cleaner to just make that a @property of the detail view controller and set that property directly in your prepareForSegue:sender:
method.
Hope that helps.
Upvotes: 1
Reputation: 4131
when you change the value in the NSUserDefaults, do synchronize. For example
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:@"4" forKey:@"codeKey"];
[defaults synchronize];
also
if (selectedCodeLocal == @"1")
should really be
if (selectedCodeLocal isEqual:@"1")
Upvotes: 2
Reputation: 47619
I think your problem is that old favourite, string comparison.
I think you mean:
if ([selectedCodeLocal isEqualToString:@"1"])
or something like it (it's been over a year since I've written Obj-C).
Upvotes: 1