任何人都明白,爲什麼在CoreDataBooks示例代碼:爲什麼此代碼使用presentModalViewController? (不pushViewController)
(一)控制器的方法交換差異
雖然按項目並進入詳細視圖使用什麼似乎是標準的UINavigationController概念「pushViewController」,即當你點擊「添加」一個新的記錄按鈕時,它啓動新的視圖,通過「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];
}
謝謝