代码之家  ›  专栏  ›  技术社区  ›  Jeffrey Berthiaume

将uiImageView添加到nsmutabledictionary

  •  0
  • Jeffrey Berthiaume  · 技术社区  · 16 年前

    我正在尝试将多个uiImageView实例添加到一个nsmutabledictionary的nsmutableArray中。

    我这样做是为了能够返回并拉出uiImageView引用,以更改这些图像的位置。

    似乎,当我从字典中提取信息时,它只返回一个空指针…

    关于我应该如何实现这一点有什么建议吗?可能有一种方法可以更容易地存储n个图像的列表,并能够在以后引用它们….

    // (for i = 0 to 30)
    
    UIImageView *newBall = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball.png"]];
    newBall.center = CGPointMake (arc4random() % 280 + 20, arc4random() % 440 + 20);
    [self.view addSubview:newBall];
    
    NSMutableDictionary *dictBall = [[[NSMutableDictionary alloc] init] autorelease];
    [dictBall setValue:newBall forKey:@"imgBall"];
    
    // the dictionary is then added to the array
    
    [newBall release];
    
    // end for loop
    

    (我用的是字典,因为我还存储了动态变化率的dy/dx值)

    1 回复  |  直到 16 年前
        1
  •  1
  •   Jordan    16 年前

    上面的代码对我来说不好。

    每次通过循环,您都会创建一个新的nsmutabledictionary*dictball。循环存在后是否有东西保持它?从代码中看不出来。这可能就是为什么你的目标是零。

    把这一行移到for循环的外面/前面,你就可以了。

    此外,是否确实要将uiviewimage存储在字典中?为什么不只是一个ID(基于标签)和坐标(cgpoint或cgrect)?这样,您就可以使用标记通过标记来查找uiImageView,并将其移动到任何您想要的位置?节省内存。