虽然我确定它们存在,但我很难找到或确定在ViewController中声明插座的官方最佳做法。
Though I'm sure they exists, I'm having difficulties finding or pinning down an official best practice for declaring outlets in a ViewController.
有3个选项据我所见:
当我尝试通过拖动到我的ViewController自动生成属性时Xcode当前崩溃来自IB,但从我记忆中,这样做会创造一个没有ivar的财产。也可以拖入ivar部分,这将创建一个没有属性的ivar。这表明仅限物业和仅使用ivar的商店都可以使用苹果。
Xcode currently crashes when I try and auto-generate a property by dragging into my ViewController from IB, but from what I remember, doing so creates a property without an ivar. It is also possible to drag into the ivar section and this will create an ivar without a property. This suggests that property-only and ivar only outlets are both OK with apple.
所以在viewDidUnload中我们需要为任何商店分配nil,但是dealloc呢。如果我们使用没有ivar的属性,我们如何释放我们的插座,我们不应该在init或dealloc中使用任何访问器?
So in viewDidUnload we need to assign nil to any of our outlets, but what about dealloc. If we have used a property without an ivar, how can we release our outlet give that we are not supposed to use any accessors in an init or dealloc?
它似乎我允许我们在没有访问器的情况下释放我们的插座的唯一模式是使用支持ivar的属性,因此我们可以在dealloc中手动释放我们的ivar而不使用其访问器,但是这是Apple代码生成的一个选项不支持。
It seems to me that the only pattern which would allow us to release our outlet without an accessor is using a property backed with an ivar, so we can manually release our ivar in dealloc without using its accessor, however this is the one option which Apple's code-generation doesn't support.
推荐答案根据经验,我通常会为 IBOutlet创建访问器 s。
As a rule of thumb, I usually create accessors for IBOutlets.
在ARC或非ARC项目中,我通常会执行以下操作:
In ARC or non-ARC projects I usually do the following:
//.h (ARC) @property (nonatomic, weak) IBOutlet UILabel* myLabel; //.h (non-ARC) @property (nonatomic, retain) IBOutlet UILabel* myLabel; //.m @synthesize myLabel;通过这种方式,您可以让编译器为您创建实例变量。但是你也可以声明你的实例变量并告诉编译器使用它。
In this manner you can let the compiler to create an instance variable for you. But you can also declare your instance variable and tell the compiler to use that.
然后你可以在任何你想要的地方使用那个访问器/实例变量。
Then you can use that accessors/instance variable wherever you want.
Apple内存管理指南说您必须避免使用 init 或 dealloc 方法中的访问者方法非ARC项目。因此,例如:
The Apple Memory Management guide says that you have to avoid accessors methods in init or dealloc methods when you have non-ARC projects. So, for example:
// (non-ARC) - (void)dealloc { [myLabel release]; myLabel = nil; // I'm using the instance variable here! [super dealloc]; }这在非ARC项目中非常重要。原因是,如果没有访问器,KVC会将nib对象分配给实例变量并对其进行保留。如果您忘记释放它,可能会发生内存泄漏。使用访问器强制您最后释放该对象。
This is very important in non-ARC projects. The reason is that, if there is no accessor, KVC will assign the nib object to the instance variable and will put a retain on it. If you forget to release it, you could have a memory leak. Using an accessor force you to release that object at the end.
我强烈建议阅读 friday-qa-2012-04-13-nib-memory-management 由Mike Ash提供。这是关于笔尖和内存管理的一篇非常酷的文章。
I strongly suggest to read friday-qa-2012-04-13-nib-memory-management by Mike Ash. It's a very cool article on nib and memory management.
希望它有所帮助。
更多推荐
IBOutlets应该是ivars还是属性?
发布评论