NSMutableArray +添加 UIImageView - 非常简单但不起作用 - arghh 帮助

发布于 2024-10-18 21:25:54 字数 1034 浏览 5 评论 0原文

我正在动态创建图像视图(一个小汉堡图标,仅供参考),并希望通过使用可变数组来跟踪它们。我在标头中将数组声明为:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

戴着白色围巾的女孩 2024-10-25 21:25:54

您不应该创建容量为 0 的数组!当您第一次向其中添加内容时,它会立即调整大小。您只需调用[NSMutableArray array]即可获取默认初始容量的数组。

至于您的实际问题:根据您所写的内容,您在 viewdidLoad 中声明了一个名为 myBurger 的新变量。即在 viewDidLoad 中,您应该具有:

self.myBurgers = [NSMutableArray array];

而不是:

NSMutableArray* myBurgers = [NSMutableArray arrayWithCapacity:0];

请注意, 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:

self.myBurgers = [NSMutableArray array];

and not:

NSMutableArray* myBurgers = [NSMutableArray arrayWithCapacity:0];

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] in dealloc.

夏末 2024-10-25 21:25:54

你必须保留你在 viewDidLoad: 中创建的数组,现在你有一个自动释放的对象,并且将在运行循环后消失。

做:

NSMutableArray* myBurgers = [[NSMutableArray arrayWithCapacity:0] retain];

You have to retain the array you making in viewDidLoad:, now you have an autoreleased object, and will be gone after the runloop.

Do:

NSMutableArray* myBurgers = [[NSMutableArray arrayWithCapacity:0] retain];
远昼 2024-10-25 21:25:54

请记住,这

NSMutableArray* myBurgers = [NSMutableArray arrayWithCapacity:0];

是一个自动释放的对象

-(void)viewDidLoad {

 myBurgers = [[NSMutableArray alloc]initWithCapacity:kMaxBurgersQty];



}

-(void)dealloc {
   [myBurgers release];
   myBurgers = nil;
}

,这将确保汉堡数组不会在运行时中间的某个地方自动释放。

Bare in mind that

NSMutableArray* myBurgers = [NSMutableArray arrayWithCapacity:0];

is an auto released object

-(void)viewDidLoad {

 myBurgers = [[NSMutableArray alloc]initWithCapacity:kMaxBurgersQty];



}

-(void)dealloc {
   [myBurgers release];
   myBurgers = nil;
}

That will grant that the burger array won't be autorelease somewhere in the middle of runtime.

如若梦似彩虹 2024-10-25 21:25:54

您正在 viewDidLoad 中创建一个新实例,而不是分配给接口成员。

You are creating a new instance in your viewDidLoad instead of assigning to the interface member.

逆光下的微笑 2024-10-25 21:25:54

如果我理解正确,您的类声明一个实例变量,如下所示:

NSMutableArray *myBurgers;

然后您按如下方式重写 viewDidLoad :

- (void)viewDidLoad
{
    // ...
    NSMutableArray *myBurgers = [NSMutableArray arrayWithCapacity:0];
    // ...
}

如果是这种情况,则您的 viewDidLoad 方法正在声明本地变量变量 myBurgers 暂时屏蔽同名的实例变量。然后将自动释放的数组分配给局部变量。该数组在 viewDidLoad 返回后不久就被释放,但无论如何它都不会分配给实例变量。

要解决此问题,请重写您的 viewDidLoad 方法,如下所示:

- (void)viewDidLoad
{
    // ...
    myBurgers = [[NSMutableArray alloc] init];
    // ...
}

自动释放分配给实例变量的对象是没有意义的;而是使用 alloc 和适当的 init... 方法。此外,正如之前的海报所指出的,初始化容量为零的可变数组是相当没有意义的。要么使用特定的非零容量(如果您事先知道数组将包含多少个元素),要么采用默认值。

If I'm understanding you correctly, your class declares an instance variable like so:

NSMutableArray *myBurgers;

You then override viewDidLoad as follows:

- (void)viewDidLoad
{
    // ...
    NSMutableArray *myBurgers = [NSMutableArray arrayWithCapacity:0];
    // ...
}

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 from viewDidLoad, but in any case it's never assigned to the instance variable.

To fix this, rewrite your viewDidLoad method as follows:

- (void)viewDidLoad
{
    // ...
    myBurgers = [[NSMutableArray alloc] init];
    // ...
}

There's no point autoreleasing objects that you're assigning to instance variables; instead use alloc and the appropriate init... 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文