Iphone 为什么UIButton不需要alloc和init?
为什么UIButton不需要alloc和init调用,而我们只使用Iphone 为什么UIButton不需要alloc和init?,iphone,ios,xcode,ipad,Iphone,Ios,Xcode,Ipad,为什么UIButton不需要alloc和init调用,而我们只使用 UIButton*myBtn=[UIButton button类型:UIButtonTypeCustom] 上述行是否自动分配并初始化uibutton 有必要发布myBtn吗?因为我没有显式地使用alloc和init 这可能是一个简单的问题,但我不知道正确的答案,有人能帮忙吗?感谢您的帮助。好的,按钮WithType将返回一种UIButton类型,该类型为alloc和init。它也是自动释放的 您可以自己使用alsalloc和i
UIButton*myBtn=[UIButton button类型:UIButtonTypeCustom]代码>
上述行是否自动分配并初始化uibutton
有必要发布myBtn吗?因为我没有显式地使用alloc和init
这可能是一个简单的问题,但我不知道正确的答案,有人能帮忙吗?感谢您的帮助。好的,按钮WithType将返回一种UIButton类型,该类型为alloc
和init
。它也是自动释放的
您可以自己使用alsalloc
和init
UIButton按钮,这将为您提供一个类型为UIButtonTypeCustom
buttonWithType
的UIButton按钮,返回一个您不必释放的自动释放对象。由于您没有alloc
,因此无需release
方法名称遵循一组规则-;)
基本上,以alloc
、new
、copy
或mutableCopy
开头的名称需要调用release
(或autorelease
)
任何其他返回对象的方法都将返回自动释放的对象
例如:
// You need to release these
NSString *myString = [[NSString alloc] init];
NSString *nextString = [myString copy];
UIbutton *button = [[UIButton alloc] initWithType:UIButtonTypeCustom];
// You don't need to release these
NSString *thirdString = [NSString stringWithString:@"Hello"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
希望有帮助 每个UIClass对象的实现都是隐藏的类级alloc方法来自NSObject如果是UIButton,我们也会得到alloc方法,如果我们写的话,但是apple Guy又给出了一个名为buttonwithtype类级方法的方法,他这样做
(id)buttonWithType:(UIButtonType)buttonType
{
UIButton=[NSObject alloc];
code for button type specification
return id;
}
这就是为什么我们不需要再次执行alloc的原因它不一定意味着自定义按钮。但是+1用于自动释放。非常感谢deanWombourne的建议,它真的很有帮助