views:

48

answers:

2

Hello future friends who is gonna help me big time on this project,

I have these Books parsed from XML file (adopted from this post I found). First view (RootViewController) has a list of book titles in a UITable. When user clicks on one of the books, instead of viewing the books detail (BooksDetailViewController) in a second UITable, I would like the Title, Author and Summary to be in a custom view laid out in Interface Builder with UILabels and UITextView.

IBOutlet UILabel *bookTitle;
IBOutlet UILabel *bookAuthor;
IBOutlet UITextView *bookSummary;

I believe my problem has to do with RootViewController.m "didSelectRowAtIndexPath". If I understand the example I had adapted properly (which I am not confident about), it passed the Book array into each row of the new table on my BooksDetailViewController. 

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

I have tried a few things to recreate  didSelectRowAtIndexPath, but I am not having much luck. Below I have the example code commented out and with my own terrible guessed code /sigh.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

     // if(bdvController == nil)
     // bdvController = [[BookDetailViewController alloc] initWithNibName:@"BookDetailView" bundle:[NSBundle mainBundle]];
     // Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
     // bdvController.aBook = aBook;
     // [self.navigationController pushViewController:bdvController animated:YES];


     NewBookDetailViewController *detailViewController = [[NewBookDetailViewController alloc] initWithNibName:@"NewBookDetailViewController" bundle:nil];
     Book *aBook = appDelegate.books;

     //detailViewController.aBook.title = bookTitle.text;
     //detailViewController.aBook.author = bookAuthor.text;
     //detailViewController.aBook.summary = bookSummary.text;

     [self.navigationController pushViewController:detailViewController animated:YES];
     [detailViewController release];
}

Do I actually connect the parsed data aBook.title to bookTitle (UILabel) in didSelectRowAtIndexPath of RootViewController? 

or 

Do I connect them in viewDidLoad in NewBookDetailViewController.m?

bookTitle.text = aBook.title;
bookAuthor.text = aBook.author;
bookSummary.text = aBook.summary;

What is the proper way to write the didSelectRowAtIndexPath in RootViewController for a custom view instead of a table view?

Please take it easy on me.

  • Clo
A: 

The purpose of NewBookDetailViewController is to display information about a single book, yes? So NewBookDetailViewController will need a way of being told which book it's supposed to display.

An easy way of doing this would be to add a property to this class to hold a reference to the Book it's supposed to display. You would not want to pass the entire array of books to the detail view controller. How is it supposed to know which single book to display?

Here' how my implementation would look:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Book *selectedBook = [appDelegate.books objectAtIndex:indexPath.row];
    NewBookDetailViewController* viewController = [[NewBookDetailViewControlleralloc] initWithNibName:@"NewBookDetailViewController"];
    viewController.book = selectedBook; // "book" is a property you'd add to NewBookDetailViewController

    [self.navigationController pushViewController:viewController animated:YES];
    [viewController release];
}

Here's how you add the book property to NewDetailViewController:

The .h file:

@class Book;
@interface NewDetailViewController : UIViewController {
    // other instance variables
    Book *_book;
}

@property (nonatomic, retain) Book *book;

@end

The .m file:

#import "NewDetailViewController.h"
#import "Book.h" // assuming the "Book" class is defined in Book.h

@implementation NewDetailViewController

@synthesize book = _book;

- (void)dealloc {
    [_book release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // set up your controls to display the book information
}

// other method implementations

@end
Alex
Thanks Alex for such a quick reply. I am going to try your suggestion right now. Before you replied I actually was gonna add a comment... - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];detailViewController.bookTitle.text = aBook.title;detailViewController.bookAuthor.text = aBook.author;detailViewController.bookSummary.text = aBook.summary;[self.navigationController pushViewController:detailViewController animated:YES];[detailViewController release];}
slowman21
DUDE! You are awesome! I think I got it!! Thanks a lot man.
slowman21
A: 

Hey Alex,

I did as you said and got my custom view. I ran into a little problem with an image now. I can show strings fine with book_title.text = _book.book_title for UILabel. But what if I need an UIImageView? I tried...

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

 book_title.text = _book.title;
 book_author.text = _book.author;
 book_summary.text = _book.summary;

 NSURL *url = [[NSURL alloc] initWithString:_book.image];
 NSData *data = [NSData dataWithContentsOfURL:url];
 UIImage *currentImage = [[UIImage alloc] initWithData:data]; 

 [book_image setImage:currentImage];

}

But this did not work. If I pass that same variable to a UILabel and just get the string. It gives the right URL path and info. book_image.text = _book.image. And if I type in a full path for initWithString:@"http://myurl.com" it works perfectly.

I really hope you see this message since you already know my initial problem and was extremely helpful.

slowman21