为什么UIButton不需要分配和初始化?

Geo*_*rge 5 iphone xcode ipad ios

为什么 UIButton 不需要分配和初始化调用,而我们只使用 UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];.

  1. 上面的行是否自动分配并初始化 uibutton?
  2. 是否有必要释放 myBtn?,因为我没有明确使用 alloc 和 init 。

这可能是一个简单的问题,但我不知道正确的答案,有人可以帮忙吗?感谢任何帮助,提前致谢。

dea*_*rne 2

方法名称遵循一组规则 -阅读此链接;)

基本上,以allocnewcopy或开头的名称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];
Run Code Online (Sandbox Code Playgroud)

希望有帮助!