这两个ViewController Push有什么区别?

发布于 2024-09-25 07:58:37 字数 945 浏览 2 评论 0原文

苹果提供的样板推送

     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
 NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 [detailViewController release];

和这个方法(来自PragProg iphone SDK开发书)

  [self.navigationController pushViewController:self.cabinetController
                                       animated:YES];

有什么区别以及区别的含义是什么?我正在使用的 SDK 与这本书...但这看起来确实不同,并且似乎意味着非常不同的所有权(如果这是正确的词的话)。如果问题太复杂而无法回答,我主要关心的是这些方法中的一种是否更具有内存效率。

编辑: 好吧,好吧,在通过发布这个问题澄清了我的视野之后……我认为没有那么大的区别。样板方法按需分配内存,而 books 方法使 Cabinet 查看属性。我认为这使得样板方法在某种程度上更好......或者等于......我仍然模糊的部分。看起来两者都会释放相同的内存,但也许内存是使用书籍方法更早保存的。

What are the differences and the implications of the differences between the boilerplate push provided by apple

     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
 NSManagedObject *selectedObject = [[self fetchedResultsController] objectAtIndexPath:indexPath];
 // ...
 // Pass the selected object to the new view controller.
 [self.navigationController pushViewController:detailViewController animated:YES];
 [detailViewController release];

and this method (from PragProg iphone SDK development Book) cabinet controller is added to interface and @synthesize in implementation:

  [self.navigationController pushViewController:self.cabinetController
                                       animated:YES];

i would chalk it up to the newness of the SDK i am working with versus the book...but that seems really different and seems to imply VERY different ownership, if that is the correct word. My main concern, if the question is too convoluted to answer, is whether or not one of these methods is more memory efficient.

EDIT:
ok, well, after clearing my vision by posting this question...i don't think there that much of a difference. the boilerplate method allocates memory on demand where the books method makes the cabinet view a property. i think that makes the boilerplate method better somehow...or equal...that part i am still fuzzy on. it seems like both will release just the same but maybe the memory is held earlier using the books method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

掩于岁月 2024-10-02 07:58:37

Apple 版本的内存效率更高,因为 detailViewController 及其视图在从导航控制器的堆栈中弹出时将被释放。与 PragProg 版本不同的是,PragProg 版本将 CabinetController 保留在实例变量中(因此可以防止其被释放),Apple 代码不会存储对其所创建的详细控制器的引用。

如果用户可能经常在 Cabinet 控制器和保留它的视图控制器之间来回导航,则 PragProg 实现可能有意义,因为这避免了因重复创建和释放对象而产生的一些 CPU 开销,但您会必须对其进行分析,看看这是否会产生任何有意义的差异(通常不会)。

The Apple version is more memory efficient, because the detailViewController and it's view will be deallocated when it gets popped from the navigation controller's stack. Unlike the PragProg version, which retains the cabinetController in an instance variable (and therefore prevents it from being deallocated), the Apple code isn't storing a reference to the detail controller it's creating.

The PragProg implementation might make sense if the user is likely to navigate frequently back and forth between the cabinet controller and the view controller that's retaining it, since that avoids a bit of CPU overhead incurred by repeatedly creating and deallocating the objects, but you'd have to profile it to see if that makes any meaningful difference (which it ordinarily wouldn't).

南街九尾狐 2024-10-02 07:58:37

在第二种情况下,cabinetController 似乎是 self 指示的类的属性。在样板中,情况并非如此。

In the second case it would appear that cabinetController is a property of the class indicated by self. In the boilerplate this is not the case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文