开发者

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 re开发者_Python百科moving the view from screen not completely deleting the object.

How can i delete the object completely?


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜