0

我希望你能帮助我弄清楚为什么一段特定的代码不能按我的意愿工作——与将图像加载到将填充网格的不同数组有关。

我的程序有一个由 16 个方格组成的网格(称为 button1、button2 等),每个方格都有一张图片,本质上是黑色图片、白色图片或绿色图片(黑白图片相同)除了每个单独的绿色图块(其中目前有 32 个是唯一的)之外的所有图块)。当用户通过屏幕上的“NEXT”或“PREVIOUS”按钮选择不同的级别时,其想法是根据先前保存到数组中的内容加载 16 个图标并使用适当的图标。

所以基本上如果图标标记为 1-16,下一个级别将是 17-32,等等。当我从级别 1 移动到级别 2 时,我想将当前屏幕上的图像保存到我调用 level1part1 的数组中,然后将数组 level1part2 的内容加载到相同的方格中。当我从 2 级回到 1 级时 - 我想将屏幕上的图像保存到 level1part2,然后将 level1part1 数组的内容加载回屏幕上的 16 个图块。

所以最初加载屏幕时,我在 viewDidLoad 方法中有以下声明:

UIImage *def = [UIImage imageNamed:@"silhouette.png"];
level1part1 = [[NSMutableArray alloc]initWithObjects:def, def, def, def, def, def, def, def, def, def, def, def, def, def, def, def, nil];
level1part2 = [[NSMutableArray alloc]initWithObjects:def, def, def, def, def, def, def, def, def, def, def, def, def, def, def, def, nil];
currentArray = [[NSMutableArray alloc]initWithObjects:def, def, def, def, def, def, def, def, def, def, def, def, def, def, def, def, nil];

其中 def 是黑色图像。我将在稍后解释当前数组的用途。

我有两种方法,称为 pressPreviousLevel 和 pressNextLevel,解释如下:

- (IBAction)pressNextLevel:(UIButton *)sender {

NSMutableArray *nextLevelArray;

if (currentlevel == 1)
{
    nextLevelArray = level1part2.mutableCopy;
    [self.nextLevel setHidden:YES];
}

我声明了一个名为 nextLevelArray 的 NSMutableArray,它将保存我想要投影到下一个屏幕上的图像的内容——在本例中为 level1part2,因为我将从 level1 转到 level2。

//// SAVING IMAGES ON SCREEN TO CURRENTARRAY
[currentArray replaceObjectAtIndex:0 withObject: button1.imageView.image];
[currentArray replaceObjectAtIndex:1 withObject: button2.imageView.image];
[currentArray replaceObjectAtIndex:2 withObject: button3.imageView.image];
[currentArray replaceObjectAtIndex:3 withObject: button4.imageView.image];

ETC

然后我用当前屏幕上的内容填充我之前声明的 currentArray 数组。

if (currentlevel == 1)
{
    level1part1 = currentArray.mutableCopy;
}

然后我说,因为我在 1 级 - 我希望 level1part1 等于这个 currentArray;有效地将屏幕上的内容保存到这个 level1part1 数组。

现在我想将 level1part2 中的内容加载到屏幕上的 16 个图块上:

// REPLACE IMAGES WITH PREVIOUS LEVELS IMAGES FROM ARRAY 
button1.imageView.image = [nextLevelArray objectAtIndex:0];
button2.imageView.image = [nextLevelArray objectAtIndex:1];
button3.imageView.image = [nextLevelArray objectAtIndex:2];
button4.imageView.image = [nextLevelArray objectAtIndex:3];

ETC

currentlevel ++;

并增加当前级别。

pressPreviousLevel 也会发生类似情况:

- (IBAction)pressPreviousLevel:(UIButton *)sender {

NSMutableArray *previousLevelArray;

if (currentlevel == 2)
{
    previousLevelArray = level1part1.mutableCopy;
    [self.previousLevel setHidden:YES];
    [self.nextLevel setHidden:NO];
}

/// SAVING IMAGES ON SCREEN TO CURRENTARRAY
[currentArray replaceObjectAtIndex:0 withObject: button1.imageView.image];
[currentArray replaceObjectAtIndex:1 withObject: button2.imageView.image];
[currentArray replaceObjectAtIndex:2 withObject: button3.imageView.image];
[currentArray replaceObjectAtIndex:3 withObject: button4.imageView.image];

等剩余的瓷砖。因此,如果我目前处于 2 级(不能处于 1 级,因为我不能从那里向后退),我声明一个名为 previousLevelArray 的 NSMutableArray,在这种情况下将保存 level1part1 的内容。然后我将我在屏幕上看到的所有 16 个图像保存到我的 currentArray 中。

if (currentlevel == 2)
{
    level1part2 = currentArray.mutableCopy;
}

现在,我询问我当前处于哪个级别并保存 currentArray,它应该包含屏幕上的所有图标,以便正确的数组 - 在这种情况下,它应该是 level1part2。

// REPLACE IMAGES WITH PREVIOUS LEVELS IMAGES FROM ARRAY
button1.imageView.image = [previousLevelArray objectAtIndex:0];
button2.imageView.image = [previousLevelArray objectAtIndex:1];
button3.imageView.image = [previousLevelArray objectAtIndex:2];
button4.imageView.image = [previousLevelArray objectAtIndex:3];

ETC

currentlevel --;

最后,我现在将来自 previousLevelArray(在本例中为 level1part1)中的图像加载回屏幕上。

所以,我的问题是这样的——假设我在 1 级的第一个图标是绿色的(我们称之为 GREEN1)——这是该级别的那个图块所独有的——当我按下下一步时,我的屏幕上会填充黑色图标,这是我想要的。但是,当我在下一个级别再次单击第一个图标时,它会变为 GREEN1 图标,这不是我想要的——它仍然被保留——这破坏了我的程序。如果我做相反的事情 - 从 2 级到 1 级 - 1 级上的第一个图标将变成 2 级上的第一个图标,也会发生同样的情况。

当您单击将更改图片的图标时,我的方法中没有任何内容,因此我确信问题出在此代码中。基本上它在屏幕上显示正确的图标,但是当我点击图标时 - 它仍然保留前一个屏幕中的一些信息 - 好像它还没有完成加载或其他什么。

所以我的问题是,任何人都可以发现代码中发生的可能导致这个问题的事情吗?我确信问题不在于代码的其他地方 - 这是对我上面打印的代码的简单修复,以使其正常工作。

提前感谢您的帮助 - 如果这里有任何“错误代码”,请告诉我 - 总是愿意学习 - 我只使用 Objective C 很短的时间。

4

1 回答 1

2

您需要使用-setImage:forState:-setBackgroundImage:forState:设置按钮的图像。

您可以为不同的控件状态设置不同的图像,例如 UIControlStateNormal、UIControlStateHighlighted 和 UIControlStateSelected。

于 2012-06-09T19:48:29.260 回答