How do I save strings in a table and display the strings in a label?

advertisements

I'm a rookie at iOS and Objective-C programming, trying (HARD) to learn it, so i've enrolled in a basic course. I'm trying to implement two IBAcions in my ViewController.m file but i seem to have problems regarding my way of thinking about this problem and the difficulties understanding the scope, e.g what i can and cannot call and such.

One of this IBActions will throw a dice, getting the right images, and another that will save the outcome and display the latest 10 throws.

Please do not get discouraged by what seems to be a lot of code.

These are the following properties i've declared in the @interface(.h file)

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIImageView *dieOne;
@property (weak, nonatomic) IBOutlet UIImageView *dieTwo;
@property (weak, nonatomic) IBOutlet UILabel *latestTenThrows;

@property (nonatomic) NSUInteger dieValue1;
@property (nonatomic) NSUInteger dieValue2;
@property (nonatomic) NSUInteger totalValue;

@property (nonatomic) NSMutableArray *rolledDiceArray;
@property (nonatomic) NSMutableString *rolledDiceString;
@end

And this is my implementation

- (IBAction)throwDice {

NSMutableArray *dieArray = [[NSMutableArray alloc] initWithObjects:
                            @"one", @"two", @"three", @"four", @"five", @"six", nil];

NSUInteger dieIndex1 = arc4random_uniform(6);
NSUInteger dieIndex2 = arc4random_uniform(6);

UIImage *image1 = [UIImage imageNamed:[dieArray objectAtIndex:dieIndex1]];
UIImage *image2 = [UIImage imageNamed:[dieArray objectAtIndex:dieIndex2]];

self.dieOne.image = image1;
self.dieTwo.image = image2;

So far so good, the right images are showing but here is where it gets tricky for me. I am trying to "save" the rolled dices value and show them in a lable like this: 5 + 2 = 7 in the view, and it should diplay the latest 10 throws.

self.dieValue1 = (dieIndex1+1);
self.dieValue2 = (dieIndex2+1);
self.totalValue = self.dieValue1 + self.dieValue2;

NSMutableString *rolledDiceString = [[NSMutableString alloc] init];

[rolledDiceString appendString:[NSString stringWithFormat:
                                 @"%d + %d = %d",
                                 self.dieValue1,self.dieValue2,self.totalValue]];

How will i get permission to this array in my other functicion, - (IBAction)saveThrow{} below?

NSMutableArray *rolledDiceArray = [[NSMutableArray alloc]init];
[rolledDiceArray addObject:rolledDiceString];

If i add this NSLog call i will only get 1, no matter how many times i hit "Roll dice" in the interface", which means that when i hit "Roll dice" in the interface it's not putting the values, created into a string, in the array correctly. How do i add the strings correctly to my array so that i later can retrieve that information from another IBAction? And do i have permission to this array in my function below?

NSLog(@"%d",rolledDiceArray.count);
} // End for -(IBAction)throwDice{

In my second and last IBAction, i'm trying to add the strings to the label, but having issues here since my array doesn't getting the strings.

- (IBAction)saveThrow {
NSMutableArray *savedThrowsArray = [[NSMutableArray alloc] init];

[savedThrowsArray addObject:self.rolledDiceArray];

self.latestTenThrows.text = [// Latest 10 items (strings) in savedThrowsArray];

}


Don't flail; think what you are doing. Your words (in your code) have meaning. The computer can only do what you tell it to do...! So, let's look:

NSMutableArray *rolledDiceArray = [[NSMutableArray alloc]init];
[rolledDiceArray addObject:rolledDiceString];

Every time you run that code, you are creating a completely new empty mutable array (that is what = [[NSMutableArray alloc] init] means in the first line). Therefore no matter how many times you run it, the resulting array will contain only one object (the object you add in the second line with addObject).

NSMutableArray *savedThrowsArray = [[NSMutableArray alloc] init];
[savedThrowsArray addObject:self.rolledDiceArray];

Same deal.

And meanwhile, you've got these completely different instance variable sitting unused:

@property (nonatomic) NSMutableArray *rolledDiceArray;
@property (nonatomic) NSMutableString *rolledDiceString;

But none of your code ever talks to / about them. They are just sitting there going to waste. You need to initialize them and put your data in them - not in these local automatic variables that you keep creating and throwing away.