【发布时间】:2011-07-03 01:03:12
【问题描述】:
任何人都明白为什么在 CoreDataBooks 示例代码中:
(a) 控制器交换差异的方法
虽然单击项目并转到详细视图使用似乎是“pushViewController”的标准 UINavigationController 概念,但当您单击“添加”新记录按钮时,它会启动通过“presentModalViewController”方法添加记录的新视图?也就是说,这两种情况下的方法不能相同,只是使用 pushViewController 方法吗?
在使用每种方法时实际上有什么优势吗?我看不太清楚。我猜苹果一定有一些东西可以为不同的场景选择这些不同的方法。例如:
对用户的任何差异(即 UI 差异或功能 差异)他们会看到?
开发人员的任何差异 (或优点/缺点)
例如,如果您要考虑在“添加”场景中使用 pushViewController 方法而不是 presentModalViewController 方法...
(b) 数据共享方式差异
他们共享公共数据对象的方法似乎不同 - 所以再次想知道为什么方法不一样? (即在这两种情况下,主控制器都暂时传递给另一个视图,并且它们之间存在一些共享数据 - 即子视图需要传递回父视图)
代码提取方便
那是“编辑”:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Create and push a detail view controller.
DetailViewController *detailViewController = [[DetailViewController alloc] initWithStyle:UITableViewStyleGrouped];
Book *selectedBook = (Book *)[[self fetchedResultsController] objectAtIndexPath:indexPath];
// Pass the selected book to the new view controller.
detailViewController.book = selectedBook;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
但是对于“添加”
- (IBAction)addBook {
AddViewController *addViewController = [[AddViewController alloc] initWithStyle:UITableViewStyleGrouped];
addViewController.delegate = self;
// Create a new managed object context for the new book -- set its persistent store coordinator to the same as that from the fetched results controller's context.
NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
self.addingManagedObjectContext = addingContext;
[addingContext release];
[addingManagedObjectContext setPersistentStoreCoordinator:[[fetchedResultsController managedObjectContext] persistentStoreCoordinator]];
addViewController.book = (Book *)[NSEntityDescription insertNewObjectForEntityForName:@"Book" inManagedObjectContext:addingContext];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addViewController];
[self.navigationController presentModalViewController:navController animated:YES];
[addViewController release];
[navController release];
}
谢谢
【问题讨论】:
标签: iphone ios uiviewcontroller uinavigationcontroller modalviewcontroller