IBOutlet collection release problem

∥☆過路亽.° 提交于 2019-12-25 01:55:57

问题


I have an array for IBOutlet collection

.h

@interface UpisiRezultat : UIViewController {
    NSArray *buttons;
}

@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *buttons;

.m

@synthesize buttons;

- (void)viewDidLoad
{
    [self setValue:[UIFont fontWithName:@"NeverSayNever" size:22] forKeyPath:@"buttons.font"];
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    buttons = nil;
}

- (void)dealloc
{
    [buttons release]; --> Error
    [super dealloc];
}

Why does my program crash when I have [buttons release]; in dealloc? Without it, it doesn't crash...


回答1:


updated(Dec1) code and Tested.

- (void)dealloc {

    self.buttons = nil;

    [super dealloc];
}

you should not release them.

http://www.bobmccune.com/2011/01/31/using-ios-4s-iboutletcollection/




回答2:


If you have made a connection to your buttons with Interface Builder, it's your view that owns it and will release it.




回答3:


Since buttons is an NSArray and it is explicitly retained, then it must be released and then set to nil in -dealloc.

See Darren's answer at: Settings IBOutlets to nil in dealloc See an IBOutletCollection example at: http://www.bobmccune.com/2011/01/31/using-ios-4s-iboutletcollection/.



来源:https://stackoverflow.com/questions/6749563/iboutlet-collection-release-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!