gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 05:07:11 25 4
gpt4 key购买 nike

任何人都明白为什么在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com