保留IBOutlet视图元素真的是最佳做法吗? [英] Really best practice to retain IBOutlet view elements?

查看:139
本文介绍了保留IBOutlet视图元素真的是最佳做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在遇到UIViewControllers的情况,其中包含大量的IBOutlets,将控制器连接到其视图的子视图(通常是UILabels)。

I keep running into situations with UIViewControllers containing a large amount of IBOutlets connecting the controller to its view's subviews (typically UILabels).

遵循最佳实践,即在所有UI元素上使用retain: @property(retain,nonatomic)UILabel * theElement1 @property(保留,非原子)UILabel * theElement2 ,...在 dealloc viewDidUnload

Following "best practices", i.e. use retain on all UI elements: @property (retain, nonatomic) UILabel *theElement1, @property (retain, nonatomic) UILabel *theElement2, ... gives me insane amounts of boiler plate code in dealloc and viewDidUnload for the view controller.

在UIViewController之外永远不会使用或设置有问题的IBOutlets(set-method仅用于viewDidUnload和加载nib时)除了加载nib时自动。

The offending IBOutlets are never used nor set outside the UIViewController (the set-method is only used in viewDidUnload and when the nib is loaded) except automatically when the nib is loaded.

最佳做法的结果是:


  • dealloc 散落着 [theElement1 release] [theElement2 release] 等。

  • viewDidUnload with [self setTheElement1:nil] [self setTheElement2:nil] etc。

  • dealloc littered with [theElement1 release], [theElement2 release] etc.
  • viewDidUnload with [self setTheElement1:nil], [self setTheElement2:nil] etc.

但是,因为所有这些元素都是已知的无论如何都要被视图保留,并且UIViewController在适当的时候发布视图,我看到 完全没有理由 让我手动管理它。

However, since all of those elements are known to be retained by the view anyway, and the view is released by the UIViewController at appropriate times, I see absolutely no reason whatsoever for me to manually manage this.

这种特殊最佳实践(据我所知)的原因是与您的保留一致。但是一旦你开始拥有大量的网点,你就更有可能错过在两种方法中的任何一种方式处理某个插座,而不是你无法正确地改变网点以保留你真正想要的那些特殊插座即使在视图再见之后也要保留。

The reason for this particular "best practice" (as far as I can tell) is to be consistent with your retains. But once you start having a large amount of outlets, you're more likely to miss handling the some outlet somewhere in either of the two methods, than you'll have trouble correctly changing outlets to "retain" for those special outlets you really want to retain even after the view is goodbye.

除了我所知道的那种最佳实践之外是否有某种原因或者我应该可以自由地打破这个在UIViewController视图的子视图的特定情况下的规则?

Is there some reason for this "best practice" other than the one I know about or should I feel quite free to break this "rule" in the particular case of subviews to an UIViewController's view?

推荐答案

你应该坚持这个最佳实践。当您在内存警告后访问IBOutlets时,它可以保护您免受非常奇怪的崩溃。是的,您需要手动管理您的IBOutlets。 Accessorizer 可以自动执行此代码。

You should stick to this best practice. It protects you from very bizarre crashes when you access IBOutlets after a memory warning. Yes, you need to manually manage your IBOutlets. Accessorizer does a nice job of automating this code.

在ObjC 2.0之前,我们必须手动编写所有的访问器(@property和@synthesize是该语言的新增功能)。事情变得更好了。随着我们转向64位ABI和垃圾收集,事情变得更加简单(你应该期望这些东西最终能够进入iPhone)。

Before ObjC 2.0, we had to write all of our accessors by hand, too (@property and @synthesize are very new additions to the language). Things have gotten a lot nicer. As we move to the 64-bit ABI and garbage collection, things get even simpler (and you should expect these things eventually to make their way to iPhone).

但是对于现在,按照 Nib对象的内存管理。您需要进行非常少量的输入以进行大量调试。 (嗯,看起来他们已经再次更新了这个文档;是时候自己学习了。)

But for now, follow the memory management rules as laid out in Memory Management of Nib Objects. You trade a really small amount of typing for a huge amount of debugging. (Hmm, looks like they've updated this doc again; time to study up myself.)

这篇关于保留IBOutlet视图元素真的是最佳做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆