Hobbit
Hobbit

Reputation: 35

Modify text attribute of UILabels in an NSArray

I have an NSMutableArray full of UILabels and I want to modify the text attribute of them by stepping through the array. I know its not a problem with the UILabels or the function generating the text I want assigned to the labels as I have tested it outside of the array and it works fine. I just cant seem to get the text attribute to modify while the UILabel is inside of an array.

I have the array populated as below:

NSMutableArray *scrambledWordLabelArray;

-(void)initUIArrays {
    [scrambledWordLabelArray insertObject:ScrambledWordLabel1 atIndex:0];
    [scrambledWordLabelArray insertObject:ScrambledWordLabel2 atIndex:1];
    [scrambledWordLabelArray insertObject:ScrambledWordLabel3 atIndex:2];
    [scrambledWordLabelArray insertObject:ScrambledWordLabel4 atIndex:3];
    [scrambledWordLabelArray insertObject:ScrambledWordLabel5 atIndex:4];
    [scrambledWordLabelArray insertObject:ScrambledWordLabel6 atIndex:5];
}

Then once the view loads its supposed to change the labels to the strings generated by the other function:

- (void)viewDidLoad
{
    [super viewDidLoad];   
    [self initUIArrays];

    currentGameList = [self GetWordList:[self LoadWordlist]];

    for (NSUInteger i = 0; i < [currentGameList count]; i++) {

For this part I found two solutions, neither worked for me...

Solution 1:

         UILabel *temp = [scrambledWordLabelArray objectAtIndex:i];
         temp.text = [self ScrambleWord:[currentGameList objectAtIndex:i]];

Solution 2:

         [[scrambledWordLabelArray objectAtIndex:i] setText:[self ScrambleWord:[currentGameList objectAtIndex:i]]];
    }
}

Any help would be greatly appreciated because I've been researching and debugging for hours with no fix :(

Upvotes: 3

Views: 360

Answers (1)

rmaddy
rmaddy

Reputation: 318774

You don't initialize the scrambledWordLabelArray. You need to add the following line:

scramledWordLabelArray = [[NSMutableArray alloc] init];

On a side note:

Is it true that currentGameList and scrambledWordLabelArray will always have the same number of elements? Your 'for' loop is based on the number of elements in the currentGameList yet you use the same index to get elements from the scrambledWordLabelArray.

Both solutions you posted are basically the same code.

Upvotes: 2

Related Questions