NSMutableArray +添加 UIImageView - 非常简单但不起作用 - arghh 帮助
我正在动态创建图像视图(一个小汉堡图标,仅供参考),并希望通过使用可变数组来跟踪它们。我在标头中将数组声明为:
NSMutableArray *myBurgers;
并在 viewDidLoad 中初始化数组
NSMutableArray* myBurgers = [NSMutableArray arrayWithCapacity:0];
这是我制作图像并推入数组的方法 - 目前我的数组计数返回 0 - 并且我编译器错误说“未使用的变量” 'myburgers'
-(void)burgerManGo:(CGPoint)location {
burgerCount=burgerCount++;
if(location.x<=(320) && location.y<=(480)){
CGRect myImageRect = CGRectMake(location.x, location.y, 65, 55);
UIImageView *myNewBurger = [[UIImageView alloc] initWithFrame:myImageRect];
[myNewBurger setImage:[UIImage imageNamed:@"burgerMan.png"]];
myNewBurger.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myNewBurger];
//but adding to my array doesn't seem to work...
[myBurgers addObject: myNewBurger];
//because this always come back as 0
NSLog(@"Array Size: %ld", [myBurgers count]);
}
}
一如既往,我们非常感谢任何帮助。
米奇。
i'm dynamically creating imageviews (of a little burger icon, fyi) and want to keep track of them by using an mutable array. I'm declaring the array in my header as:
NSMutableArray *myBurgers;
And init my array in my viewDidLoad
NSMutableArray* myBurgers = [NSMutableArray arrayWithCapacity:0];
Here is my method for making the images and shoving into an array - currently my array count is returning 0 - and i've compiler error saying 'Unused variable 'myburgers'
-(void)burgerManGo:(CGPoint)location {
burgerCount=burgerCount++;
if(location.x<=(320) && location.y<=(480)){
CGRect myImageRect = CGRectMake(location.x, location.y, 65, 55);
UIImageView *myNewBurger = [[UIImageView alloc] initWithFrame:myImageRect];
[myNewBurger setImage:[UIImage imageNamed:@"burgerMan.png"]];
myNewBurger.opaque = YES; // explicitly opaque for performance
[self.view addSubview:myNewBurger];
//but adding to my array doesn't seem to work...
[myBurgers addObject: myNewBurger];
//because this always come back as 0
NSLog(@"Array Size: %ld", [myBurgers count]);
}
}
Any help, as ever, is much appreciated.
Mickey.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不应该创建容量为 0 的数组!当您第一次向其中添加内容时,它会立即调整大小。您只需调用
[NSMutableArray array]
即可获取默认初始容量的数组。至于您的实际问题:根据您所写的内容,您在 viewdidLoad 中声明了一个名为 myBurger 的新变量。即在 viewDidLoad 中,您应该具有:
而不是:
请注意, self.myBurger = 会导致您的新数组被保留(假设您将 myBurgers 属性定义为保留,而不是分配)--这就是你想要的。不要忘记在
dealloc
中[myBurgers release]
。You shouldn't create an array with capacity 0! It will instantly resize the first time you add something to it. You can just call
[NSMutableArray array]
to get an array of default initial capacity.As for your actual problem: from what you've written, you're declaring a new variable inside viewdidLoad called myBurger. i.e. in viewDidLoad, you should have:
and not:
Note that the
self.myBurger =
will cause your new array to be retained (assuming you defined the myBurgers property to be retain, and not assign) -- which is what you want. Don't forget to[myBurgers release]
indealloc
.你必须保留你在 viewDidLoad: 中创建的数组,现在你有一个自动释放的对象,并且将在运行循环后消失。
做:
You have to retain the array you making in viewDidLoad:, now you have an autoreleased object, and will be gone after the runloop.
Do:
请记住,这
是一个自动释放的对象
,这将确保汉堡数组不会在运行时中间的某个地方自动释放。
Bare in mind that
is an auto released object
That will grant that the burger array won't be autorelease somewhere in the middle of runtime.
您正在
viewDidLoad
中创建一个新实例,而不是分配给接口成员。You are creating a new instance in your
viewDidLoad
instead of assigning to the interface member.如果我理解正确,您的类声明一个实例变量,如下所示:
然后您按如下方式重写 viewDidLoad :
如果是这种情况,则您的 viewDidLoad 方法正在声明本地变量变量
myBurgers
暂时屏蔽同名的实例变量。然后将自动释放的数组分配给局部变量。该数组在viewDidLoad
返回后不久就被释放,但无论如何它都不会分配给实例变量。要解决此问题,请重写您的
viewDidLoad
方法,如下所示:自动释放分配给实例变量的对象是没有意义的;而是使用
alloc
和适当的init...
方法。此外,正如之前的海报所指出的,初始化容量为零的可变数组是相当没有意义的。要么使用特定的非零容量(如果您事先知道数组将包含多少个元素),要么采用默认值。If I'm understanding you correctly, your class declares an instance variable like so:
You then override
viewDidLoad
as follows:If that's the case, your
viewDidLoad
method is declaring a local variable,myBurgers
that temporarily masks the instance variable with the same name. You then assign an autoreleased array to the local variable. The array is deallocated shortly after the return fromviewDidLoad
, but in any case it's never assigned to the instance variable.To fix this, rewrite your
viewDidLoad
method as follows:There's no point autoreleasing objects that you're assigning to instance variables; instead use
alloc
and the appropriateinit...
method. Also, as a previous poster noted, initializing a mutable array with a capacity of zero is fairly pointless. Either use a specific non-zero capacity (if you know in advance how many elements the array will contain), or else take the default.