你好未来的朋友,他们将帮助我在这个项目上大放异彩,
我从XML文件解析了这些书籍(从我发现的这篇文章中采用)。第一个视图(RootViewController)有一个UITable中的书名列表。当用户点击其中一本书,而不是在第二个UITable中查看书籍详细信息(BooksDetailViewController)时,我希望标题,作者和摘要位于Interface Builder中使用UILabels和UITextView的自定义视图中。
IBOutlet UILabel *bookTitle;
IBOutlet UILabel *bookAuthor;
IBOutlet UITextView *bookSummary;
我相信我的问题与RootViewController.m“didSelectRowAtIndexPath”有关。如果我理解了我已经适当调整的例子(我对此并不自信),它将Book数组传递到BooksDetailViewController上新表的每一行。
Book *aBook = [appDelegate.books objectAtIndex:indexPath.row];
我尝试了一些东西来重新创建didSelectRowAtIndexPath,但我没有太多运气。下面我将示例代码注释掉,并用我自己可怕的猜测代码/叹息。
- (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];
}
我是否真的将解析后的数据aBook.title连接到RootViewController的didSelectRowAtIndexPath中的bookTitle(UILabel)?
或
我是否在NewBookDetailViewController.m中的viewDidLoad中连接它们?
bookTitle.text = aBook.title;
bookAuthor.text = aBook.author;
bookSummary.text = aBook.summary;
在RootViewController中为自定义视图而不是表视图编写didSelectRowAtIndexPath的正确方法是什么?
请放轻松我。
答案 0 :(得分:0)
NewBookDetailViewController
的目的是显示有关单本书的信息,是吗?所以NewBookDetailViewController
需要一种方式告诉它应该显示哪本书。
这样做的一种简单方法是向该类添加一个属性,以保存对它应该显示的Book
的引用。您不希望将整个图书集传递给详细视图控制器。怎么知道要显示哪一本书呢?
以下是我的实施方式:
- (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];
}
以下是将book
属性添加到NewDetailViewController
的方法:
.h
文件:
@class Book;
@interface NewDetailViewController : UIViewController {
// other instance variables
Book *_book;
}
@property (nonatomic, retain) Book *book;
@end
.m
文件:
#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