取消分配和删除UiButton [英] Deallocating and removing UiButtons

查看:103
本文介绍了取消分配和删除UiButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下命令动态创建按钮的程序:

I am trying to make a program that dynamically creates a button using the command:

[UIButton buttonWithType:UIButtonTypeRoundedRect]

但是当我使用这些命令删除我创建的按钮:

But when I use these commands the delete the button I create:

[currentButton removeFromSuperview];
[currentButton dealloc];
[currentButton release];

我收到错误。

推荐答案

在Objective-C / Cocoa框架中,你遇到了两种不同的方式来接收对象:你已经显式分配了内存(通过构造函数)和你已经接受内存引用(通过类方法)。

In the Objective-C/Cocoa framework, you encounter two different ways to receive objects: ones which you have explicitly allocated memory for (via a constructor) and ones that you have received memory reference to (via a class method).

FooBar *fone = [[FooBar alloc] initWithText:@"Hello, World!"];

在本例中,通过调用显式地为对象分配内存,使用alloc方法,然后使用initWithText方法用数据初始化,该方法将具有如下的方法头:

In this example, memory is explicitly being allocated for the object by your call, using the alloc method, and then it is being initialized with data using the initWithText method that would have a method header like this:

- (id)initWithText:(NSString *)text;

另一方面,你也会遇到类自动为你创建的对象。下面是一个示例:

On the other hand, you also will encounter objects that are created by classes automatically for you. An example of this would be below:

FooBar *ftwo = [FooBar fooBarWithWelcomeText];

在这个例子中,一个FooBar对象被返回,即使我们不是调用alloc分配内存它。有很多不同的原因来实现这样的方法,但它主要完成从抽象某些细节从使用对象的代码。上面的例子将有一个相应的方法头如下:

In this example, a FooBar object is being returned even though we are not calling alloc to allocate memory for it. There are many different reasons to implement the method like this, but its mainly done to abstract certain details from the code that is using the object. The above example would have a corresponding method header like this:

+ (FooBar *)fooBarWithWelcomeText;

根据使用的方法,它会更改如何与对象的内存交互。因此,对于第一个例子,在为对象分配内存后,它的保留计数为1.如果你使用该对象,你需要使用

Depending on which approach is used, it changes how you interact with the memory of the object. So for the first example, after allocating the memory for the object you receive it back with a retain count of 1. If you are done using the object, you need to explictly release it with

[fone release];

在第二个示例中,您将收到一个自动释放对象一旦自动回流泵被排空,则被解除分配。如果你想保留它,你必须明确保留:

In the second example, you are receiving an autoreleased object, which will be deallocated once the autoreleasepool is drained. If you want to keep it, you must explicitly retain with:

[ftwo retain];

如果您不希望保留,可以直接保留,自动。你可以告诉一个方法使用autorelease有两个特点:1)当你收到对象时你不会使用alloc;和2)方法标题旁边将有一个+。这意味着该方法被声明为一个类方法(类似于Java静态方法)。

If you do not wish to retain it, you can just leave it as is and it will be deallocated automatically. You can tell a method uses autorelease by two characteristics: 1) you will not utilize alloc when you receive the object; and 2) there will be a "+" next to the method heading. This means that the method is declared as a class method (similar to Java static methods).

为了最终回答你的具体情况,你只需要确保保持计数降低到1(唯一有对它的引用的对象是自动释放池)。在你的例子中,它将这样做:

So to finally answer your specific situation, you only need to make sure that the retain count is lowered to 1 (The only object having a reference to it was the autorelease pool). In your example, it would be done like this:

UIButton *currentButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[someView addSubView:currentButton];

// some code later
[currentButton removeFromSuperview];

您不需要发布语句,因为您从未明确保留它。当您将UIButton添加到另一个视图时,它保留了该对象,因此它将引用计数增加到2.通过从视图中删除它,它被降低到1,以便当自动释放池被刷新时,您的UIButton将被释放。 Btw,从来不直接调用dealloc方法。当对象的保留计数减少到0时,将自动调用dealloc方法。

You do not need the release statements because you never explicitly retained it. When you added the UIButton to another view, it retained the object so it incremented the reference count to 2. By removing it from the view, it is lowered back down to 1 so that when the autorelease pool is flushed, your UIButton will be deallocated. Btw, never call the dealloc method directly. When the retain count of an object is decremented to 0, the dealloc method will automatically be called.

这篇关于取消分配和删除UiButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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