为什么UIButton不需要alloc和init? [英] Why UIButton does not require to alloc and init?

查看:131
本文介绍了为什么UIButton不需要alloc和init?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么UIButton不需要alloc和init调用,而只使用
UIButton * myBtn = [UIButton buttonWithType:UIButtonTypeCustom];

Why UIButton does not require to alloc and init call instead we use only UIButton *myBtn = [UIButton buttonWithType:UIButtonTypeCustom];.


  1. 以上行是否自动分配s并初始化uibutton ??

  2. 是否有必要释放myBtn ?,因为我没有明确地使用alloc和init。

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

This may be a simple question but i don't know the correct answer for this, does anybody can help? Any help is appreciated,thanks in advance.

推荐答案

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

There's a set of rules that the method names follow - read this link ;)

基本上,名称以 alloc 开头,复制 mutableCopy 要求您调用 release (或 autorelease )。

Basically, names beginning with alloc, new, copy or mutableCopy require you to call release (or autorelease).

返回对象的任何其他方法都将返回自动释放的对象。

Any other methods that return an object will return autoreleased objects.

例如:

// 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];

希望有所帮助!

这篇关于为什么UIButton不需要alloc和init?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆