变量图像名称和UIImageView

时间:2014-09-02 20:58:34

标签: xcode variables uiimageview

因此,我为每个级别引入3个变量,为它们分配临时值,并希望根据值更改图像。

for (NSInteger row = 1; row < 11; row++) {  // 1 to 10

//Recall Key Name and save the Data Stored to new array
    NSArray *allLevelStatsArray = [[NSUserDefaults standardUserDefaults] objectForKey:_allDataStats];

    _tempLevel = [allLevelStatsArray[0] intValue];   // = 1
    _tempPoints = [allLevelStatsArray[1] intValue];  // = 500
    _tempStar = [allLevelStatsArray[2] intValue];    // = 2

   // For each level, assign the level image the correct star image
   // _top3Stars_Level1 for Levels 1 - 10;   Stars 0 - 3
   // Hard coding, this works
   _top3Stars_Level1.image = [UIImage imageNamed: @"2-stars.png"];

   // Create the level image to assign the stars
   NSString *top3StarsLevel = [NSString stringWithFormat:@"_top3Stars_Level%ld",(long)_tempLevel];  // Returns _top3Stars_Level1
   // Create the star image based on the stars
   NSString *top3StarsImage = [NSString stringWithFormat:@"%ld-stars.png",(long)_tempStar];  // Returns 2-stars.png

   // Doesn't work
   top3StarsLevel.image = [UIImage imageNamed:top3StarsImage];

   // Doesn't work
   top3StarsLevel = [NSString stringWithFormat:top3StarsImage];

我觉得我已经接近了,但现在已经有一段时间了。

1 个答案:

答案 0 :(得分:1)

虽然有一些事情我可能会改变您构建数据的方式(即我可能会为每个&#34;级别&#34;使用字典来识别级别,分数,星级等等)我认为这可能会让你走上正轨:

// I'm assuming you have properties/instance variables to reference these image views... Either way, you're going
// to want to reference them in order when you iterate through the levels, so I would recommend putting them in
// an array like this:
NSArray *levelImageViewArray = @[_top3Stars_Level1, _top3Stars_Level2, _top3Stars_Level3, _top3Stars_Level4, _top3Stars_Level5, _top3Stars_Level6, _top3Stars_Level7, _top3Stars_Level8, _top3Stars_Level9, _top3Stars_Level10];

// using your current model, there's an array for each level, and each array holds the level, the score (we're ignoring
// the score for now) and the number of stars
NSArray *levelStatsArray = @[@[@1, @11, @1], @[@2, @22, @2], @[@3, @33, @3], @[@4, @1, @3], @[@5, @22, @2], @[@6, @33, @3], @[@7, @1, @1], @[@8, @22, @2], @[@9, @33, @2], @[@10, @33, @3]];

// loop through the level stats array, starting at 0
for (NSInteger index = 0; index < levelStatsArray.count; index++) {
    // get the stats sub-array for the current level
    NSArray *arrayForLevel = levelStatsArray[index];

    // get the current level and star value, which are the first and third values respectively 
    // in the sub array, and convert the NSNumbers to their NSString values
    NSString *currentLevel = [arrayForLevel[0] stringValue];
    NSString *starsForThisLevel = [arrayForLevel[2] stringValue];

    // generate the stars filename from the 3rd value in the array for this level
    NSString *starsFilename = [NSString stringWithFormat:@"%@-stars.png", starsForThisLevel]];

    // log the info
    NSLog(@"level: %@, starsFilename: %@", currentLevel, starsFilename);

    // create the level and star image from the level/star strings
    UIImage *starsImage = [UIImage imageNamed:starsFilename];

    // get a reference to the specific image view you have for this level
    UIImageView *imageViewForThisLevel = levelImageViewArray[index];
    [imageViewForThisLevel setImage:starsImage];
}

产生了以下输出:

level: 1, starsFilename: 1-stars.png
level: 2, starsFilename: 2-stars.png
level: 3, starsFilename: 3-stars.png
level: 4, starsFilename: 3-stars.png
level: 5, starsFilename: 2-stars.png
level: 6, starsFilename: 3-stars.png
level: 7, starsFilename: 1-stars.png
level: 8, starsFilename: 2-stars.png
level: 9, starsFilename: 2-stars.png
level: 10, starsFilename: 3-stars.png