1

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

I receive an error. How would I go about removing and deallocating the memory from a uibutton?

jcb344
  • 323
  • 5
  • 16

4 Answers4

2

I got this problem long time ago, please notice that

[UIButton buttonWithType:UIButtonTypeRoundedRect]

has autorelease inside, so in your initialisation you need to do retain, like this:

_mybutton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];

and later point, you can do:

[_mybutton release];

Hope helps :)

kororo
  • 1,972
  • 15
  • 26
0

You can't not dealloced the UIButton which you get through buttonWithType from system,if you don't alloced any instance then you are not entitled to call release on that.

In your case, you can use removeFromSuperview but not either dealloc or release .

You can't call dealloc directly on object, this is invoked by the system when you say release on object.

EDITED:

you could create you button using initWithFrame function of UIView. But you will get only the button type UIButtonTypeCustom which is by default, and also can't not change the button type because it's readonly property. So you would get the rounded button by using your some images.

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76
0

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!"];

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

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;

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

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).

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

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.

adimitri
  • 1,296
  • 9
  • 13
-1

You're not supposed to call dealloc directly. Try removing the dealloc line and see how that works.

Chad
  • 382
  • 1
  • 11