Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID

update label when returning from detail view

gbennagbenna Posts: 127Registered Users @ @
I have a tableview in which the detail text is loaded from a plist unless a certain file exists in the documents directory.
code
if ([cell.textLabel.text isEqualToString:@"Child"])  {
if([[NSFileManager defaultManager] fileExistsAtPath: documentsDirectory23]) {
cell.detailTextLabel.text = self.user1.text;/*[NSString stringWithContentsOfFile:documentsDirectory23 encoding:NSUTF8StringEncoding error:NULL];*/
}
else {
cell.detailTextLabel.text = [row objectForKey:@"detailText"];

}
}
this works fine, however.
When the user taps on the cell and goes to the detail view they enter various names and the tap buttons to select that name to be "signed in". I have it so that when a user "signs in" a file is saved to the documents directory that is then used to display the detailed text in the cell. As each user "signs in" they overwrite the existing file in the documents directory.
code;
NSArray *documentPaths5 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory5 = [documentPaths5 objectAtIndex:0];
NSString *documentTXTPath8 = [documentsDirectory5 stringByAppendingPathComponent:@"UserSignedIn.txt"];

NSString *savedString1 = userTwo.text;
NSString *addString = [savedString1 stringByAppendingString:@" is signed in."];
NSLog(@"The Save file is:%@", savedString1);
[savedString1 writeToFile:documentTXTPath5 atomically:YES
encoding:NSUTF8StringEncoding error:nil];
[addString writeToFile:documentTXTPath8 atomically:YES encoding:NSUTF8StringEncoding error:nil];
What I want to do and am having difficulties doing is having the UILabel update when the user returns from the detail view so it reflex the new saved file in the documents directory.

I have tried reloading the tableview in viewWillAppear.
-(void) viewWillAppear {
[self.tableView reloadData];
}
I have tried to set the label to nil when the detailed view is pushed into view.
else if ([vc isKindOfClass:[selfViewController class]])
{
self.user1= nil ;

}
as well in other places but to no avail. can someone help me with this. I would also like this to happen in another place with a UIImageView.

Thanks

Replies

  • BrianSlickBrianSlick Treadmill Desk Ninja Northern Virginia / DCPosts: 10,075Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    if ([cell.textLabel.text isEqualToString:@"Child"])
    You should never be relying on the contents of the cell. Refer to your data model instead.
    cell.detailTextLabel.text = [row objectForKey:@"detailText"];
    "row" is a really bad name for a dictionary, especially in a table view delegate method.

    And just in general, you shouldn't be loading data in cellForRow. Prep your data in viewWillAppear and reload the table.
  • gbennagbenna Posts: 127Registered Users @ @
    I understand your concerns and I will be working at these changes, however this doesn't address the original question of updating the text in a UILabel upon going back to the main view when the information is changed in a detail view. Any Ideas on how to do this. I want to do this with a UILabel and a UIImageView
  • BrianSlickBrianSlick Treadmill Desk Ninja Northern Virginia / DCPosts: 10,075Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
  • Duncan CDuncan C Posts: 8,994Tutorial Authors, Registered Users @ @ @ @ @ @ @
    gbenna said:

    I understand your concerns and I will be working at these changes, however this doesn't address the original question of updating the text in a UILabel upon going back to the main view when the information is changed in a detail view. Any Ideas on how to do this. I want to do this with a UILabel and a UIImageView

    Brian is giving you good information (as usual).

    You should never use view objects to store state data. Your model should save state data, and you should query your model to figure out state.

    Say you have an NSArray tableData, and each entry in the array is a dictionary. You'd use code like this:

    NSDictionary *cellData = [tableData objectAtIndex: rowIndex];
    NSString *detailText = [cellData objectForKey: @"detailText"];

    if ([detailText isEqualToString:@"Child"])
    {
    //whatever.
    }

    If you want to change the data at a given row, you should change the data in your model array at that row, then tell the tableview to reload that row of the table using the tableview method reloadRowsAtIndexPaths:withRowAnimation: (or you can simply reload the entire table.) You can also fetch the cell using the table view method cellForRowAtIndexPath: (not the table view data source method tableView:cellForRowAtIndexPath:) and update the data directly if you get back a non-nil cell.

    In your detail view you should change the data in your model (the tableData array, in my example). Then the master table view will update its contents when the table is reloaded.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • gbennagbenna Posts: 127Registered Users @ @
    Ok I think maybe I wasn't clear or I am not understanding. I am saying that if the main text of a cell is something (choosing the cell I want) and if the file has been saved for the first time then I want to the detail text to show that saved file text. Otherwise I want to show the text in the plist that populates that rest of the tableview.

    Is there someway to change just the text in the plist for that cell?

    Here is my code below for my tableView set up.

    Maybe you can point out where I need to change things and put in the reload.

    - (void)viewDidLoad{
    [super viewDidLoad];
    NSString *documentsDirectory39 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *documentaryDirectory239 = [documentsDirectory39 stringByAppendingPathComponent:@"UserSignedIn.txt"];

    if([[NSFileManager defaultManager] fileExistsAtPath: documentaryDirectory239]) {
    self.user1.text =[NSString stringWithContentsOfFile:documentaryDirectory239 encoding:NSUTF8StringEncoding error:NULL];
    }

    self.user1.hidden = YES;
    NSLog(@"user1;%@",self.user1.text);

    NSString *textName2 =@"imagename.txt";

    NSArray *paths3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
    NSUserDomainMask, YES);
    NSString *documentsDirectory31 = [paths3 objectAtIndex:0];
    NSString *slash = [documentsDirectory31 stringByAppendingPathComponent:@"/"];
    NSString *documentsDirectory2 = [slash stringByAppendingPathComponent:self.user.text];

    //Get a full path to the image in the documents directory.
    NSString *fullPath3 = [documentsDirectory2 stringByAppendingPathComponent:textName2];
    self.number.text =[NSString stringWithContentsOfFile:fullPath3 encoding:NSUTF8StringEncoding error:NULL];

    if ([self.number.text intValue]==100) {
    aboutViewController *about = [[aboutViewController alloc] initWithNibName:@"aboutViewController" bundle:nil];
    [self.tabBarController setModalTransitionStyle:UIModalTransitionStyleCoverVertical ];
    [self.tabBarController presentViewController:about
    animated:YES completion:NULL];
    };

    NSLog(@"%@",number.text);

    self.number.hidden = YES;


    UIBarButtonItem *backButton =
    [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"BACK", nil)
    style: UIBarButtonItemStyleBordered

    target: nil
    action: nil];
    self.navigationItem.backBarButtonItem = backButton;


    NSString *path = [[NSBundle mainBundle] pathForResource:NSLocalizedString(@"TABLEDATAMAINVIEW", nil) ofType:@"plist"];
    self.tableDataMainView = [NSArray arrayWithContentsOfFile:path];

    self.title = NSLocalizedString(@"TITLE1", nil);

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    }
    }

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {

    // create the parent view that will hold header Label
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 280.0, 44.0)];

    // create the button object
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor yellowColor];
    headerLabel.highlightedTextColor = [UIColor lightGrayColor];
    /*headerLabel.textAlignment = UITextAlignmentCenter;*/
    [headerLabel setTextAlignment:NSTextAlignmentCenter];
    headerLabel.font = [UIFont boldSystemFontOfSize:20];
    headerLabel.frame = CGRectMake(0.0, 0.0, 300.0, 44.0);

    // If you want to align the header text as centered
    // headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
    const NSDictionary *const sectionData = [self.tableDataMainView objectAtIndex:section];
    /*return [sectionData objectForKey:@"header"];*/
    headerLabel.text =[sectionData objectForKey:@"header"];// i.e. array element
    [customView addSubview:headerLabel];

    return customView;
    }

    - (NSString *)tableView:(UITableView *)tableView
    titleForHeaderInSection:(NSInteger)section {
    const NSDictionary *const sectionData = [self.tableDataMainView objectAtIndex:section];
    return [sectionData objectForKey:@"header"];
    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.tableDataMainView count];
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    const NSDictionary *const data = [self.tableDataMainView objectAtIndex:section];
    const NSArray *const rows = [data objectForKey:@"rows"];

    return [rows count];
    }


    - (NSDictionary *)rowForIndexPath:(NSIndexPath *)indexPath {
    const NSDictionary *const data = [self.tableDataMainView objectAtIndex:indexPath.section];
    const NSArray *const rows = [data objectForKey:@"rows"];
    return [rows objectAtIndex:indexPath.row];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {
    const NSDictionary *const row = [self rowForIndexPath:indexPath];
    NSString *documentsDirectory31 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *documentsDirectory23 = [documentsDirectory31 stringByAppendingPathComponent:@"UserSignedIn.txt"];

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
    }
    NSString *cellTitle = [row objectForKey:@"text"];

    cell.textLabel.text = [row objectForKey:@"text"];
    /*cell.detailTextLabel.text = [row objectForKey:@"detailText"];*/
    /*cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;*/
    if ([cellTitle isEqualToString:@"Child"]) {
    if([[NSFileManager defaultManager] fileExistsAtPath: documentsDirectory23]) {
    cell.detailTextLabel.text = self.user1.text;/*[NSString stringWithContentsOfFile:documentsDirectory23 encoding:NSUTF8StringEncoding error:NULL];*/
    }
    else {
    cell.detailTextLabel.text = [row objectForKey:@"detailText"];

    }
    NSLog(@"celltitle:%@",cellTitle);
    NSLog(@"user1;%@",user1.text);
    }

    // This assume the image file you specify exists in your bundle of course!
    NSString *imageFileName = [row objectForKey:@"image"];
    cell.imageView.image = [UIImage imageNamed: imageFileName];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.textColor = [UIColor yellowColor];
    if (indexPath.row % 2) {

    cell.backgroundColor = [UIColor redColor];
    }
    else {
    cell.backgroundColor = [UIColor blueColor];
    }
    return cell;
    }
  • Duncan CDuncan C Posts: 8,994Tutorial Authors, Registered Users @ @ @ @ @ @ @
    gbenna said:

    Ok I think maybe I wasn't clear or I am not understanding. I am saying that if the main text of a cell is something (choosing the cell I want) and if the file has been saved for the first time then I want to the detail text to show that saved file text. Otherwise I want to show the text in the plist that populates that rest of the tableview.

    Is there someway to change just the text in the plist for that cell?

    Here is my code below for my tableView set up.

    Maybe you can point out where I need to change things and put in the reload.



    - (void)viewDidLoad{
    [super viewDidLoad];
    NSString *documentsDirectory39 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *documentaryDirectory239 = [documentsDirectory39 stringByAppendingPathComponent:@"UserSignedIn.txt"];

    if([[NSFileManager defaultManager] fileExistsAtPath: documentaryDirectory239]) {
    self.user1.text =[NSString stringWithContentsOfFile:documentaryDirectory239 encoding:NSUTF8StringEncoding error:NULL];
    }

    self.user1.hidden = YES;
    NSLog(@"user1;%@",self.user1.text);

    NSString *textName2 =@"imagename.txt";

    NSArray *paths3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
    NSUserDomainMask, YES);
    NSString *documentsDirectory31 = [paths3 objectAtIndex:0];
    NSString *slash = [documentsDirectory31 stringByAppendingPathComponent:@"/"];
    NSString *documentsDirectory2 = [slash stringByAppendingPathComponent:self.user.text];

    //Get a full path to the image in the documents directory.
    NSString *fullPath3 = [documentsDirectory2 stringByAppendingPathComponent:textName2];
    self.number.text =[NSString stringWithContentsOfFile:fullPath3 encoding:NSUTF8StringEncoding error:NULL];

    if ([self.number.text intValue]==100) {
    aboutViewController *about = [[aboutViewController alloc] initWithNibName:@"aboutViewController" bundle:nil];
    [self.tabBarController setModalTransitionStyle:UIModalTransitionStyleCoverVertical ];
    [self.tabBarController presentViewController:about
    animated:YES completion:NULL];
    };

    NSLog(@"%@",number.text);

    self.number.hidden = YES;


    UIBarButtonItem *backButton =
    [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"BACK", nil)
    style: UIBarButtonItemStyleBordered

    target: nil
    action: nil];
    self.navigationItem.backBarButtonItem = backButton;


    NSString *path = [[NSBundle mainBundle] pathForResource:NSLocalizedString(@"TABLEDATAMAINVIEW", nil) ofType:@"plist"];
    self.tableDataMainView = [NSArray arrayWithContentsOfFile:path];

    self.title = NSLocalizedString(@"TITLE1", nil);

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionMiddle];
    }
    }

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {

    // create the parent view that will hold header Label
    UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 280.0, 44.0)];

    // create the button object
    UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    headerLabel.backgroundColor = [UIColor clearColor];
    headerLabel.opaque = NO;
    headerLabel.textColor = [UIColor yellowColor];
    headerLabel.highlightedTextColor = [UIColor lightGrayColor];
    /*headerLabel.textAlignment = UITextAlignmentCenter;*/
    [headerLabel setTextAlignment:NSTextAlignmentCenter];
    headerLabel.font = [UIFont boldSystemFontOfSize:20];
    headerLabel.frame = CGRectMake(0.0, 0.0, 300.0, 44.0);

    // If you want to align the header text as centered
    // headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
    const NSDictionary *const sectionData = [self.tableDataMainView objectAtIndex:section];
    /*return [sectionData objectForKey:@"header"];*/
    headerLabel.text =[sectionData objectForKey:@"header"];// i.e. array element
    [customView addSubview:headerLabel];

    return customView;
    }

    - (NSString *)tableView:(UITableView *)tableView
    titleForHeaderInSection:(NSInteger)section {
    const NSDictionary *const sectionData = [self.tableDataMainView objectAtIndex:section];
    return [sectionData objectForKey:@"header"];
    }
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.tableDataMainView count];
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    const NSDictionary *const data = [self.tableDataMainView objectAtIndex:section];
    const NSArray *const rows = [data objectForKey:@"rows"];

    return [rows count];
    }


    - (NSDictionary *)rowForIndexPath:(NSIndexPath *)indexPath {
    const NSDictionary *const data = [self.tableDataMainView objectAtIndex:indexPath.section];
    const NSArray *const rows = [data objectForKey:@"rows"];
    return [rows objectAtIndex:indexPath.row];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {
    const NSDictionary *const row = [self rowForIndexPath:indexPath];
    NSString *documentsDirectory31 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *documentsDirectory23 = [documentsDirectory31 stringByAppendingPathComponent:@"UserSignedIn.txt"];

    static NSString *CellIdentifier = @"CellIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
    }
    NSString *cellTitle = [row objectForKey:@"text"];

    cell.textLabel.text = [row objectForKey:@"text"];
    /*cell.detailTextLabel.text = [row objectForKey:@"detailText"];*/
    /*cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;*/
    if ([cellTitle isEqualToString:@"Child"]) {
    if([[NSFileManager defaultManager] fileExistsAtPath: documentsDirectory23]) {
    cell.detailTextLabel.text = self.user1.text;/*[NSString stringWithContentsOfFile:documentsDirectory23 encoding:NSUTF8StringEncoding error:NULL];*/
    }
    else {
    cell.detailTextLabel.text = [row objectForKey:@"detailText"];

    }
    NSLog(@"celltitle:%@",cellTitle);
    NSLog(@"user1;%@",user1.text);
    }

    // This assume the image file you specify exists in your bundle of course!
    NSString *imageFileName = [row objectForKey:@"image"];
    cell.imageView.image = [UIImage imageNamed: imageFileName];
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.detailTextLabel.textColor = [UIColor yellowColor];
    if (indexPath.row % 2) {

    cell.backgroundColor = [UIColor redColor];
    }
    else {
    cell.backgroundColor = [UIColor blueColor];
    }
    return cell;
    }

    I find your code pretty confusing, frankly, and am too tired to try to puzzle it out.

    A plist is a file, not an in-memory data structure.

    The problem with reading data from a plist is that all the objects are read in immutable form. If you write a mutable array of mutable dictionaries to a plist, you get back an immutable array of mutable dictionaries.

    If you have a model that can change, you need to convert those immutable structures to mutable as you read them.

    Then you can use your array structure as the source for your table view, and when the user changes the data for an entry in your table, you can change the array data at that index (or indexPath/ row & column, if your data is 2 dimensional) and then reload the cell that changed or just reload the entire table view.

    Any time your program has code that says "if the text in the text label of this cell equals some value..." you have a structural problem. Your table view cells are view objects. They present information to the user. You should not store state data in table view cells, or any view object. Ever. No exceptions. If you need to check the state of an entry in your table (not your table view, but the data table that is displayed in the table view) then you should check the values in your model object, not your view object.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • gbennagbenna Posts: 127Registered Users @ @
    So let me see if I understand what you are saying.

    1. If I have a text label or some other entity which displays information to the user (cell.textLabel, UIImageView, etc.) I shouldn't use it in an if statement.

    2. But I can use the saved file that is used to populate the text label in an if statement.

    3. So instead of saying if (cell.textlabel.text = @"whatever") I should say

    NSString *cellTitle = [row objectForKey:@"text"]; this gets the information from the stored file (plist)
    if (cellTitle isEqualToString @"whatever") this is ok because I am looking at the saved information in the plist.
    {can I now say what the cell.detailTextLabel.text =the file in the documentsDirectory can be here?} since I am loading it from some file data or a string.

    The reason I am using plists is I have this localized for five different languages.

    Is this ok? And if so how can I have the tableview reload to reflect the new saved file info?
  • BrianSlickBrianSlick Treadmill Desk Ninja Northern Virginia / DCPosts: 10,075Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    NSString *cellTitle = [row objectForKey:@"text"]; this gets the information from the stored file (plist)
    No. This gets the information from a dictionary. You may have populated the dictionary with a file originally, but after you do that, the file no longer matters. This is getting information from a file:
    self.user1.text =[NSString stringWithContentsOfFile:documentaryDirectory239 encoding:NSUTF8StringEncoding error:NULL];
    But after you read the file, the contents are in memory. What is in memory will be independent of what is in the file. This is an important distinction. Changing the stuff in memory does not affect the file unless you save again. If something else were to change the file, that would not affect the stuff in memory unless you read it again.
  • Duncan CDuncan C Posts: 8,994Tutorial Authors, Registered Users @ @ @ @ @ @ @
    gbenna said:

    So let me see if I understand what you are saying.

    1. If I have a text label or some other entity which displays information to the user (cell.textLabel, UIImageView, etc.) I shouldn't use it in an if statement.

    2. But I can use the saved file that is used to populate the text label in an if statement.

    3. So instead of saying if (cell.textlabel.text = @"whatever") I should say

    NSString *cellTitle = [row objectForKey:@"text"]; this gets the information from the stored file (plist)
    if (cellTitle isEqualToString @"whatever") this is ok because I am looking at the saved information in the plist.
    {can I now say what the cell.detailTextLabel.text =the file in the documentsDirectory can be here?} since I am loading it from some file data or a string.

    The reason I am using plists is I have this localized for five different languages.

    Is this ok? And if so how can I have the tableview reload to reflect the new saved file info?


    Based on the code I've seen, it looks like you're trying to combine localized, read-only text with data that your user can change in one big plist. Don't do that. If you want to save your localized text into a plist and load the appropriate file at runtime, create a plist that ONLY contains read-only, localized text, and use that for display.

    Create another, in-memory, mutable data structure that holds the contents of your tables that your user can edit. That will be the model for your table view. Then when the user changes their data, update the in-memory data structure and tell your table view to reload. Then save the user data to disk when you want it to persist.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • gbennagbenna Posts: 127Registered Users @ @
    Ok I think I understand. [row objectForKey@"text"] is making a dictionary from the plist and puts it in memory.
    The same is true of self.user1.text = [NSString stringWithContentsOfFile:documentaryDirectory239 encoding:NSUTF8StringEncoding error:NULL];
    I am getting the info from the documents Directory and putting it in memory which is loaded into the label user1.text.

    Since this info is now in memory, even if I change the file that I got it from (when I change it in the detailView) it won't change in memory until update it by reading it again.

    So I have to reload the label from the file and reload the tableView.

    I have done this with a rightBarButtonItem. it acts like a "refresh" reloading the tableView and reloading the info from documents Directory. However I would prefer to have it automatically done when the user exits the detailView and returns to the MainView. I tried putting code that I have in my button into the ViewWillAppear but that didn't work.

    Am I starting to get what you're saying and can you suggest some way to accomplish what I want or should I stick with the refresh button?

    this is my code in the rightBarButton for refresh. It does change the detail text in the tableView cell.
    - (IBAction)Facebook: (id) sender;{

    self.user1.text = nil;

    NSString *documentsDirectory39 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *documentaryDirectory239 = [documentsDirectory39 stringByAppendingPathComponent:@"UserSignedIn.txt"];
    self.user1.text = [NSString stringWithContentsOfFile:documentaryDirectory239 encoding:NSUTF8StringEncoding error:NULL];
    NSLog(@"user1:%@",user1);

    [self.tableView reloadData];
    }
  • BrianSlickBrianSlick Treadmill Desk Ninja Northern Virginia / DCPosts: 10,075Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited February 2013
    gbenna said:

    Ok I think I understand. [row objectForKey@"text"] is making a dictionary from the plist and puts it in memory.

    No! Read the code. "Hey, row dictionary, give me the object for the 'text' key". This has nothing to do with a plist. I repeat: this has nothing to do with a plist. Nothing is being made here. If the dictionary does not already exist, row will be nil, and this code won't do anything. Assuming it does already exist, then you could have populated it manually in code, you could have populated it with information downloaded from a server, you could have populated it from a local file, or any other number of possibilities. The fact that you used a plist is completely irrelevant to what this code does. It retrieves an object for a key, period. Done. Nothing is created here. Nothing is loaded here. The plist is not involved at this point.

    Think of it like this: Take a piece of paper, and write "The dog is red" on it. This represents your file on disk. Now you memorize this phrase. So in your head, you have "The dog is red", and the piece of paper still says "The dog is red". I then steal your piece of paper, so you can't see it anymore. Now I ask you for the 4th word, and you give me the answer: red. Now you change your mind and decide the dog should be blue, so you change what you have memorized to "The dog is blue". Again I ask you for the 4th word, and you can tell me it is blue. What does the piece of paper say? It still says red. Just because you changed your mind does not change what the paper says. So if you want the paper to say blue, then you will have to get the paper back from me to make the correction. But while I took your paper, I changed the word to green. How can you know that? You will have to read the paper again. But until you do, the paper says green, and you have blue in your head. What you have in your head was first determined by the paper, but after that they are not related in any way. If the paper changes, you have to read it again. If you change your mind, you have to write the paper again.
    gbenna said:

    The same is true of self.user1.text = [NSString stringWithContentsOfFile:documentaryDirectory239 encoding:NSUTF8StringEncoding error:NULL];
    I am getting the info from the documents Directory and putting it in memory which is loaded into the label user1.text.

    The difference here is you are directly using the file. Again, read the code. "stringWith***ContentsOfFile***". It's right there in the name.
    gbenna said:

    Since this info is now in memory, even if I change the file that I got it from (when I change it in the detailView) it won't change in memory until update it by reading it again.

    Yes.
    gbenna said:

    So I have to reload the label from the file and reload the tableView.

    If the way you are storing data involves saving/reloading the file, then yes. It doesn't have to be done this way, that is a design choice you are making.
    gbenna said:

    I have done this with a rightBarButtonItem. it acts like a "refresh" reloading the tableView and reloading the info from documents Directory. However I would prefer to have it automatically done when the user exits the detailView and returns to the MainView. I tried putting code that I have in my button into the ViewWillAppear but that didn't work.

    You have not shown any viewWillAppear code yet, and I have no reason to assume that what you did was correct.
    gbenna said:

    Am I starting to get what you're saying and can you suggest some way to accomplish what I want or should I stick with the refresh button?

    this is my code in the rightBarButton for refresh. It does change the detail text in the tableView cell.

    - (IBAction)Facebook: (id) sender;{

    self.user1.text = nil;

    NSString *documentsDirectory39 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *documentaryDirectory239 = [documentsDirectory39 stringByAppendingPathComponent:@"UserSignedIn.txt"];
    self.user1.text = [NSString stringWithContentsOfFile:documentaryDirectory239 encoding:NSUTF8StringEncoding error:NULL];
    NSLog(@"user1:%@",user1);

    [self.tableView reloadData];
    }
    Ok, you are loading data from a file, and then reloading the table. So if this code works here, then there is no reason it can't work in viewWillAppear, assuming there is a reason for the file to have different information at that time.
    Post edited by BrianSlick on
  • gbennagbenna Posts: 127Registered Users @ @
    Is viewWillAppear when I come back from a detail view to the main view using the "back button" when the detail View is loaded from a selected cell
  • BrianSlickBrianSlick Treadmill Desk Ninja Northern Virginia / DCPosts: 10,075Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited February 2013
    Put a log in it and see when it fires.

    Edit:

    I just noticed this from your first post:
    -(void) viewWillAppear {
    [self.tableView reloadData];
    }
    This is not the correct method name.
    Post edited by BrianSlick on
  • gbennagbenna Posts: 127Registered Users @ @
    Ok will use -(viewWillAppear) : (BOOL) annimated{}
  • gbennagbenna Posts: 127Registered Users @ @
    Thanks works like a charm. Will use this on other views that change info on previous views.
Sign In or Register to comment.