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

  • Home
  • SEARCH
  • 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 7543089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:20:13+00:00 2026-05-30T08:20:13+00:00

I’m trying to save some data and call it back in a tableview from

  • 0

I’m trying to save some data and call it back in a tableview from a different controller but it doesn’t working. I’m somehow losing a variable value as well, like the category var changes back to zero when I change it in a view controller, any of them.

In my NewEntry.m I have:

-(IBAction)saveButton:(id)sender {    

    int i = selectedSegment.selectedSegmentIndex;    

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    [userDefaults setInteger:i  forKey:@"category"];

    [userDefaults synchronize];

    if (selectedSegment.selectedSegmentIndex == 0) {
        [userDefaults setObject:titlefield.text forKey:@"titletexthomework"];
        [userDefaults setObject:detailstextfield.text forKey:@"detailshomework"];
    }
    else if(selectedSegment.selectedSegmentIndex == 1) {
        [userDefaults setObject:titlefield.text forKey:@"titletextprojects"];
        [userDefaults setObject:detailstextfield.text forKey:@"detailsprojects"];

    }
    else if (selectedSegment.selectedSegmentIndex == 2){
        [userDefaults setObject:titlefield.text forKey:@"titletextappointments"];
        [userDefaults setObject:detailstextfield.text forKey:@"detailsappointments"];
    }
    else if (selectedSegment.selectedSegmentIndex == 3){
        [userDefaults setObject:titlefield.text forKey:@"titletextevents"];
        [userDefaults setObject:detailstextfield.text forKey:@"detailsevents"];

    }

    else if (selectedSegment.selectedSegmentIndex == 4){
        [userDefaults setObject:titlefield.text forKey:@"titletexttodolist"];
        [userDefaults setObject:detailstextfield.text forKey:@"detailstodolist"];
    }

    [userDefaults synchronize];
    NSLog(@"selected segment %i", i);
}

then in my Projects.m I have:

-(void)viewDidLoad
{
    [super viewDidLoad];    

    categoryselected = [[NSUserDefaults standardUserDefaults] integerForKey:@"category"];
    NSLog(@"category selected %i", categoryselected);        

    titlestring = [[NSUserDefaults standardUserDefaults]  objectForKey:@"titletextprojects"];
    detailsstring = [[NSUserDefaults standardUserDefaults] objectForKey:@"detailsprojects"];        

    tabledata = [[NSArray alloc] initWithObjects:titlestring, nil];
    tablesubtitles = [[NSArray alloc] initWithObjects:detailsstring, nil];    
}

//-------------------------------------------------------
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{    
    UITableViewCell *cell = nil;

    cell = [tableView dequeueReusableCellWithIdentifier:@"projectscell"];   

    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"projectscell"];
    }

    cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [tablesubtitles objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    cell.textLabel.backgroundColor = [ UIColor clearColor ];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

    return cell;
}

*******UPDATE**************

I Changed the part that populates the table to static string like this:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
tabl = [[NSArray alloc] initWithObjects:@"hello", nil];
NSLog(@"tabledata %@", tabledata );
tab = [[NSArray alloc] initWithObjects:@"hello2", nil];
NSLog(@"details %@", tablesubtitles);



UITableViewCell *cell = nil;

cell = [tableView dequeueReusableCellWithIdentifier:@"projectscell"];


if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"projectscell"];

}
cell.textLabel.text = [tabl objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [tab objectAtIndex:indexPath.row];
cell.textLabel.font = [UIFont systemFontOfSize:14.0];
cell.textLabel.backgroundColor = [ UIColor clearColor ];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];

And the table still shows up blank.
Thanks For the help.

  • 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-30T08:20:14+00:00Added an answer on May 30, 2026 at 8:20 am

    I don’t notice anything wrong in the NSUserDefaults code, per se, though it’s only grabbing the values when the view loads, not when the values are updated. To fix that, you could send a notification to let all other interested view controllers aware of the state change. In -[NewEntry saveButton:], after saving the values in NSUserDefaults, add

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ValuesChanged"
                                                        object:self];
    

    to send a notification. Somewhere in Projects.m (init is a good place), subscribe to the notification with

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(valuesChanged:)
                                                 name:@"ValuesChanged"
                                               object:nil];
    

    This causes the notification center to call back to your valuesChanged: method when the notification is posted. We’ll be updating the UI in pretty much the same way we did in viewDidLoad, so let’s factor that code out:

    - (void)reloadData
    {
        categoryselected = [[NSUserDefaults standardUserDefaults] integerForKey:@"category"];
        NSLog(@"category selected %i", categoryselected);
    
        // XXX - note the following strings are returned autoreleased. If they're
        // stored in ivars, it's a good idea to retain them, even though we know
        // they're retained by the arrays below.
    
        titlestring = [[NSUserDefaults standardUserDefaults]  objectForKey:@"titletextprojects"];
        detailsstring = [[NSUserDefaults standardUserDefaults] objectForKey:@"detailsprojects"];
    
        [tabledata release];
        tabledata = [[NSArray alloc] initWithObjects:titlestring, nil];
    
        [tablesubtitles release];
        tablesubtitles = [[NSArray alloc] initWithObjects:detailsstring, nil];
    
        [tableView reloadData];
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self reloadData];
    }
    
    - (void)valuesChanged:(NSNotification*)notification
    {
        [self reloadData];
    }
    

    One last thing: all those strings, the keys for NSUserDefaults and the notification name @”ValuesChanged”? They really should be defined as constants so that there’s only one “true” version of the string. First, this saves you from the case where you accidentally misspell it in one place and wonder why the values aren’t syncing up. It can take hours to figure out what’s going on when that happens. (Speaking from experience here.) Second, the compiler can then check that you have the right name and the IDE’s autocomplete (when it actually works) can suggest key names for you. You can do a #define in a shared header somewhere

    #define kDefaultsKeySelectedCategory @"category"
    

    and the linker will probably create a single constant instance of the string shared between every place it’s used. Still, if I change the string in that define and Xcode’s being cranky and doesn’t recompile every source file that uses it, we’re back to the case where the key is spelled differently in different places. No good. A fancier way to do this that ensures there’s only one copy of the string is to declare

    extern NSString* const kDefaultsKeySelectedCategory;
    

    in the header file, then

    NSString* const kDefaultsKeySelectedCategory = @"category";
    

    in the .m file. Another thing I like about this way is it hides the implementation details. Nobody needs to know what the specific string is, so it shouldn’t be in the header file.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.