Stateful
Stateful

Reputation: 737

Appropriate way to delete/release a UIView after removeFromSuperview

I'm playing around with drawing in iOS apps. I have a class that is a subclass of UIView that draws some lines and stuff. When the user presses a button, I instantiate the class and do an addSubView on the view of the main UIViewController of the app. The more times the user presses that button, the more instances of that class get added to the view. It's working just fine.

Now I want to provide the user a way to delete one of those views. So far I've put a [self removeViewFromSuperview] into the touchesBegan method of the custom UIView. So when the user presses the drawing it gets removed from the view. But, it's not actually deleted, right? Since the view was instantiated within the method that executes when the button is pressed I have no way to reference it from within the UIViewController. What's the appropriate way to make sure I'm not wasting memory with a UIView that was created and removed?

On a related note, if I was to put a toggle switch on the main window's UIView that toggles delete, how can I check from within touchesBegan if that toggle switch is set to delete=yes? Would I have a some sort of boolean variable in the AppDelegate that I can check from within the UIView subclass? How would I reference that?

Thank you for your help,
Stateful

Upvotes: 0

Views: 3003

Answers (4)

djromero
djromero

Reputation: 19641

If you add the view like this:

UIView *viewBeingAdded = [[[UIView alloc] init] autorelease];
[view addSubview:viewBeingAdded];

You can remove it without leaking memory:

[theViewAboutToBeRemoved removeFromSuperview];

Regarding the UISwitch, you don't need to keep its value anywhere unless you need it for something else. You can access its value directly:

if ([theSwitch isOn]) { ... }

You don't even need an IBOutlet, you can access the switch with its tag:

UISwitch *theSwitch = (UISwitch *)[view viewWithTag:<# switch tag number #>];
if ([theSwitch isOn]) { ... }

In this case you must set a unique tag number for the switch in Interface Builder or when you create it.

Upvotes: 4

David Dunham
David Dunham

Reputation: 8329

I typically autorelease a view after adding it, leaving the parent the only reference.

As to checking a toggle, you could add an IBOutlet so you can inspect it directly. (This may not be pure MVC, but I don't know if putting it in [UIApplication sharedApplication].delegate is necessarily cleaner.)

Upvotes: 0

zaph
zaph

Reputation: 112857

If you create the UIView with alloc/init, add it to the superview then release the view, the superview will retain it. When it is removed with removeViewFromSuperview it will be dealloc'ed.

Upvotes: 0

Jim Rhodes
Jim Rhodes

Reputation: 5085

When you do [mainView addSubView:myView], mainView will retain myView. If you created myView with alloc/init, then you retained it also. If you don't need myView after adding it to the main view then simply do [myView release] after adding it. When you remove it from the main view, it will get released and deallocated.

Upvotes: 0

Related Questions