简体   繁体   English

保留IBOutlet视图元素真的是最佳做法吗?

[英]Really best practice to retain IBOutlet view elements?

I keep running into situations with UIViewControllers containing a large amount of IBOutlets connecting the controller to its view's subviews (typically UILabels). 我一直遇到UIViewControllers的情况,其中包含大量的IBOutlets,将控制器连接到其视图的子视图(通常是UILabels)。

Following "best practices", ie 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. 遵循“最佳实践”,即在所有UI元素上使用retain: @property (retain, nonatomic) UILabel *theElement1@property (retain, nonatomic) UILabel *theElement2 @property (retain, nonatomic) UILabel *theElement1 @property (retain, nonatomic) UILabel *theElement2 ,...在dealloc给我疯狂的锅炉板代码量viewDidUnload用于视图控制器。

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. 违规IBOutlets永远不会被使用,也不会在UIViewController之外设置(set-method仅用于viewDidUnload和加载nib时),除非在加载nib时自动执行。

The result from "best practice" is: “最佳实践”的结果是:

  • dealloc littered with [theElement1 release] , [theElement2 release] etc. dealloc散落着[theElement1 release][theElement2 release]等。
  • viewDidUnload with [self setTheElement1:nil] , [self setTheElement2:nil] etc. viewDidUnload[self setTheElement1:nil][self setTheElement2:nil]等。

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. 但是,由于所有这些元素都已被视图保留,并且UIViewController在适当的时候释放了视图,因此我完全没有理由手动管理它。

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. 但是,一旦你开始拥有大量的网点,你就更有可能错过在两种方法中的任何一种方式处理某个插座,而不是你无法正确地改变网点以“保留”你真正想要的那些特殊插座即使在观看结束后再保留。

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? 这个“最佳实践”除了我所知道的之外还有什么理由,或者在UIViewController视图的子视图的特定情况下我是否可以自由地打破这个“规则”?

You should stick to this best practice. 你应该坚持这个最佳实践。 It protects you from very bizarre crashes when you access IBOutlets after a memory warning. 当您在内存警告后访问IBOutlets时,它可以保护您免受非常奇怪的崩溃。 Yes, you need to manually manage your IBOutlets. 是的,您需要手动管理您的IBOutlets。 Accessorizer does a nice job of automating this code. Accessorizer可以很好地自动执行此代码。

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

But for now, follow the memory management rules as laid out in Memory Management of Nib Objects . 但是现在,请遵循Nib Objects的Memory Management中列出的内存管理规则。 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.) (嗯,看起来他们已经再次更新了这个文档;是时候自己学习了。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM