使用(或不使用)内存创建UIButton [英] Create a UIButton with (or without) memory
问题描述
我正在尝试添加带代码的UIButton(不是Interface Builder)。
I'm trying to add a UIButton with code (not Interface Builder).
有些例子说你必须为按钮分配和释放内存。
Some examples say you MUST alloc and release memory for the button.
其他人使用buttonWithType并神奇地创建一个按钮而根本不分配任何内存。
Others use buttonWithType and magically create a button without alloc'ing any memory at all.
这怎么可能?
(两者似乎都运行良好。)
(Both seem to work fine.)
我想要使用以下两种方法中的哪一种...什么时候? 1种方法或其他方法是否有一些巨大的好处?
Which of the 2 methods do I want to use... and when? Are there some huge benefits to 1 method or another?
PLEASE 不要告诉我只是去阅读文档。文档是我在这里的原因。他们似乎很少解释事情而不会遗漏大量的遗漏信息。
PLEASE don't tell me to just go "read the docs". The docs are the REASON I am here. They rarely seem to explain things without leaving out tons of 'missing info'.
推荐答案
buttonWithType:
确实使用内存,但自动释放
。这意味着将来某个时候它将发布
。所以 [UIButton buttonWithType:]
相当于 [[[UIButton alloc] initWithFrame:] autorelease]
。
buttonWithType:
does use memory, but it is autoreleased
. This means that it will be released
at some point in the future. So [UIButton buttonWithType:]
is equivalent to [[[UIButton alloc] initWithFrame:] autorelease]
.
您可以使用任何一种方法,具体取决于您是否要明确或不发布。
You can use either method, depending on whether you want to be explicit or not about releasing.
这篇关于使用(或不使用)内存创建UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!