简体   繁体   中英

How do you release objects that are off screen on iOS with ARC?

I move some UIButtons off screen and I can still enumerate them when I go through the subviews. I want to "release" them to make the script run faster, but how do I do that on iOS with ARC?

When you're under ARC, all you need to do to "release" an object is set all references to it to nil .

In your case, unless you are keeping all of these UIButton s in an array or something somewhere, you only need to remove them from their super view when they are off screen. Of course, you will have to re-add them later if you need them again

With ARC you cannot call dealloc, release, or retain, although you can still retain and release CoreFoundation objects. You unfortunately cannot manually release an object when using ARC. Did you tried weak or strong referenced objects?

I doubt you will see any performance impact unless you have an awful lot of buttons, or your enumeration block takes a ton of time per button.

Anyway, you can remove any element from it's superview with...

[view removeFromSuperView];

Thank you for all your respond but the correct answer to my question (in releasing instance objects. I should have put that in the question...sorry guys) are as follow:

for (int x =0; x<[toBeDeletedArray count]; x++) {
     UIButton* __weak butt = (UIButton*)[toBeDeletedArray objectAtIndex:x];
     [butt removeFromSuperview];
     butt = nil;
     }

The key point is to declare it "__weak", then it can be set to nil.

Thanks for all your inspiration!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM