iphone - 为什么这段代码使用 presentModalViewController? (不是 pushViewController)

标签 iphone ios uiviewcontroller uinavigationcontroller modalviewcontroller

任何人都明白为什么在 CoreDataBooks 示例代码中:

(a) Controller 交换差异的方法

虽然单击一个项目并转到详细 View 似乎使用了“pushViewController”的标准 UINavigationController 概念,但当您单击“添加”新记录按钮时它会启动新 View 通过“presentModalViewController”方法添加记录?也就是说,难道两种情况下的方法都不能相同,只是使用 pushViewController 方法吗?

在使用的地方使用每种方法实际上有什么优势吗?我不太明白。我猜想苹果一定有什么东西可以针对不同的场景选择这些不同的方法。例如:

  1. 对用户的任何差异(即 UI差异或功能 他们会看到的差异)?

  2. 开发人员的任何差异 (或优点/缺点)

例如,如果您考虑在“添加”场景中使用 pushViewController 方法而不是 presentModalViewController 方法...

(b) 数据共享方式差异

他们共享公共(public)数据对象的方法似乎有所不同 - 所以再次想知道为什么方法不一样? (即在这两种情况下,主 Controller 都暂时转移到另一个 View ,并且它们之间有一些共享数据——即 subview 需要传回给父 View )

方便的代码提取

那是为了“编辑”:

- (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];

}

谢谢

最佳答案

您使用模态视图 Controller 将用户的注意力集中在任务上。当您推送时,用户处于某种导航流程中,但仍然可以轻松获得整个应用程序。他们可能会决定前进或后退,切换到中间的不同选项卡,等等。当他们获得模态视图 Controller 时,他们无法执行任何操作,直到任务完成或取消(模态视图被取消)

关于iphone - 为什么这段代码使用 presentModalViewController? (不是 pushViewController),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5096193/

相关文章:

ios - ios 7 Storyboard上的自定义标签栏

ios - 呈现一个背景昏暗的 View Controller

iphone - 使用ios查找像素的坐标

iphone - 如何对文本进行自动换行?

ios - 更改未选择的选项卡栏图标的颜色

iphone - 从 OpenURL 呈现模态视图 Controller

ios - 条形图未缩放至 BarChartView 的大小

iphone - 音频服务功能帮助

ios - 如何在 iOS 中设置 Tableview Cell 之间的空间?

ios - 如何获取 iOS 设备型号(例如 A1530)?