gpt4 book ai didi

ios - 释放分配给 UIBar 的 NSArray

转载 作者:行者123 更新时间:2023-11-28 17:43:17 25 4
gpt4 key购买 nike

在我的应用程序中,我想制作数组,添加到工具栏然后释放它。但是,当我发布它时,我的应用程序崩溃了。为什么这样...?如何省略它?

UIImage *button1Image = [UIImage imageNamed:@"button1Image.png"];
cameraToolbar = [[UIToolbar alloc] init]; //declared in .h
UIBarButtonItem *button1 = [[ UIBarButtonItem alloc ] initWithTitle: @"qwerty" style: UIBarButtonItemStyleBordered target: self action: @selector(doAction)];
[button1 setImage:button1Image];
//same method to add flexItem and button2Image

NSArray *items = [NSArray arrayWithObjects: button1, flexItem, button2, nil];
[cameraToolbar setItems:items animated:NO];
self.view = cameraToolbar;

[items release]; // here it crashes, why? How to fix?
[button1 release];
[button2 release];
[flexItem release];
[button1Image release]; // here i get "Incorrect decrement of the reference count
//of an object that is not owned at this point by the caller"
[button2Image release];

最佳答案

[NSArray arrayWithObjects:] 是一个类方法,它返回一个数组,该数组是自动释放的对象。所以你不应该释放它。。 p>

如果您使用诸如 alloc、copy、retain 之类的方式创建对象,那么只有您有责任释放它们。

NSArray *items=[[NSArray alloc]initWithObjects:button1, flexItem, button2, nil];


............



[items release];

像这样的 button1Image 对象。保重

关于ios - 释放分配给 UIBar 的 NSArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7240621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com