1

I would have more information about ARC and weak and strong reference :

Actually, if I have :

@interface
@property (weak) IBOutlet UIButton * button
@property (weak) UIView *subview
@end

@implementation

-(BOOL) viewDidLoad
{
    UIView *aSubView= [[UIView alloc]....];
    [self.view addSubview:aSubview];
    self.subview = aSubview;
}

It's normal to have weak reference for the button because its superview have a strong reference on it.

Now, I add UIView programmatically, I also put a weak reference because when I will add this subView in a superview, there will be a strong reference. First question : Is this a good method ?

Now my real problems are on the second source code with the collection. What can I put with IBOutletCollection?

And if I want to keep an array of views which are added programmatically I can't because NSArray keep strong reference and views' superview too so there will be some leaks . How can I have a NSArray of my subviews without leaks ?

@property (?) IBOutletCollection .....
@property (?) NSArray *subviews

-(BOOL) viewDidLoad
{
    ?
}
4

3 回答 3

2

Outlets are usually weak references because the views are owned by their superviews. If you make them a weak reference, all you have to do to get rid of a view object is remove it from it's superview, and the outlet gets zeroed out.

Your example of a subview that you create programmatically is the same thing, and making it weak is a good idea.

Your NSArray of subviews needs to be a strong reference or the array will be released. Same goes with an IBOutletCollection, which is really just an array maintained by the system.

You will need to remove your views from these arrays yourself if you want them to be released prior to the owning view controller being released.

You could probably create your own equivalent of an outlet collection using an NSPointerArray, which does not retain the pointers that you pass to it. However, you would need to be careful because it also does not zero out items that are released, so you would get zombies if you removed items from your view but did not remove their entry in the NSPointerArray.

All things considered, I would suggest just using a regular mutable array and doing your own housekeeping on the contents to remove items from the array if you remove them from their superview.

于 2013-10-24T01:08:33.817 回答
0

To your first question, yes that is the recommended strategy for subview properties. As to the second question, I'm not exactly sure what you are asking. Here's a good tutorial on IBOutletCollection if that's what you're looking for. If not, please clarify what you're problem with IBOutletCollection is.

于 2013-10-24T01:04:01.730 回答
0

1) When you add a subview programatically, you can keep a weak reference as a property in your view controller.

2) IBOutletCollection is actually removed by the preprocessor and doesn't mean anything to the compiler. It's just a hint within XCode that there's an outlet collection associated with the property.

3) Here's a page that talks about using NSArray to store weak references:

NSArray of weak references (__unsafe_unretained) to objects under ARC

于 2013-10-24T01:05:11.707 回答