user930195
user930195

Reputation: 432

How to delete a UIView Object from screen?

I have created a sample app where i have created some class inherited from NSObject class for defining different atributes and properties of my object . Now i have a subclass a UIView and also able to show that object in my iphone screen.

Now i have a problem whenever i want to delete a triangle i generally call [self removeFromSuperview];and it remove the view from my screen.

But it is just removing the view from screen not completely deleting the object.

How can i delete the object completely?

Upvotes: 1

Views: 1757

Answers (1)

Cthutu
Cthutu

Reputation: 8907

From what I gather from your description - how about:

[self removeFromSuperview];
[self release];

Does that work? Alternatively, you should put this kind of logic in your view controller. Then, is you have a reference to object (say, myObject), then use:

[myObject removeFromSuperview];
[myObject release];

Removing from the super view does not mean delete this object. Your object can very live without being connected to a UIView hierarchy.

EDIT: (to flesh out Peter Deweese's comment)

When you create an UIView you can do this:

MyView* view = [[MyView alloc] initWithFrame:myViewRect];
[mySuperView addChild:view];
[view release];

This means that you no long own the view, but the super view does. The super view retains your view when its added as a child, and you relinquish it with release. Later, when you do:

[self removeFromSuperview];

The retain count will drop to 0 and your object will be deleted automatically.

Good idea Peter!

Upvotes: 2

Related Questions