tags:

views:

151

answers:

1

I am trying to release the label text each time the person click on a book title from the table view, it should change the detailViewController label (titleLabel) however it keeps showing the same book title.

Wondering If i have done something wrong - well I know I have but wondering how I fix it...

//
//  BookDetailViewController.m

#import "BookDetailViewController.h"
#import "Book.h"

@implementation BookDetailViewController

@synthesize aBook, titleLabel;

// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
    [super viewDidLoad];
    [self bookDes];
    self.title = @"Book Detail";
}

-(void)bookDes {
    [self.titleLabel setText:nil];

    [self.titleLabel setText:aBook.title];
}

- (void)dealloc {
    [aBook release];
    [titleLabel release];
    [super dealloc];
}
@end
A: 

You are calling [self bookDes] from viewDidLoad... This method is called after a view controller has loaded its associated views into memory. How are you creating the BookDetailViewController? If you only create it once and then reuse the controller each time a user presses a book title, the viewDidLoad method will also only be called once.

If you already have the book title in your parent controller, why don't you just set the property from there when you push the child onto the navigation controller?

bookDetailsController.titleLabel.text = selectedBook.title;

EDIT FROM COMMENT:

Yes, the BookDetailsViewController is created once, then saved... so the viewDidLoad is only called once.

One thing you could try is setting the label in the parent's didSelectRowAtIndexPath method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic -- create and push a new view controller

    if(bdvController == nil)
        bdvController = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:[NSBundle mainBundle]];

    Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];

    bdvController.aBook = aBook;
    bdvController.titleLabel.text = aBook.title;


    [self.navigationController pushViewController:bdvController animated:YES];
}

there are better ways to do this like overriding the setter on the details controller and setting the label... but you should keep it simple and get it working first.

Hope this helps

Ryan Ferretti
I am using a example code, from www.iPhoneSDKArticles.com. called BookDetailViewControllerHowever my issue is I don't want to show the Books in a table view after the user has click on the title for more information.I looked through that code to find what you said above but can't find it. - http://www.iphonesdkarticles.com/2008/11/parsing-xml-files.html
ALL I can say is your the KING!It worked