Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7085865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:28:40+00:00 2026-05-28T07:28:40+00:00

I have an if statement that’s condition is passed to this implementation file via

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:28:41+00:00Added an answer on May 28, 2026 at 7:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have LINQ statement that looks like this: return ( from c in customers
I have a sql statement that uses Difference => http://msdn.microsoft.com/en-us/library/ms188753.aspx I do this in
I have a select statement that is used in a gridview via a stored
I have a statement that looks like this: <asp:CheckBox ID=CheckBoxProcess Checked='<%#Eval(processedField) %>' OnCheckedChanged=CheckBoxProcess_CheckedChanged runat=server
I have a statement that looks something like this if not @user.verified? or @user.credit_card.expired?
In SQL server 2008, I have select statement that spits below text output. 'text1d','text2','text3'
I have an INSERT statement that looks like this: INSERT INTO officer (officer_number, name,
I have a SELECT statement that looks like this: SELECT * FROM photos, p_votes
I have this statement that im trying to figure out how to use in
I have a case statement that goes a little something like this: case @type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.