0

I am working on an educational game for a local school system, in this game students are presented with six numbers and they have to try and use as many of them as they can to equal a seventh number, the numbers are stored in an array called currentHand, and the numbers need to be displayed as text on buttons present on the screen. The catch is, as they combined numbers together the values on the buttons need to change accordingly. Say I add two buttons a button with the value 1 and a button with a value 2, the value three gets added to the array and the values 1 and 2 are deleted, then one button needs to become invisible and the other needs to be updated with the value 3. I was told to try this with an IBOutletCollection, but because I am still very new to objective C, I have no idea how to use an IBOutCollection to do this, I had an idea to create a method that would tie each button to an element in the array, as the list shrank in size so would the number of buttons, so if you had the numbers

{1,2,3,4,5,6}

each button would have one value assigned to them, but as the list changed to say

{1,2,3,4,11}

the button displaying the number 6 would disappear and the button holding the number 5 would change to 11, but as I said I have no idea how to accomplish this.

As I cannot display pictures being such a new member, I have linked one to how I tried to set up the IBOutletCOllection

and below is the code that instantiates currentHand

-(NSMutableArray *) currentHand{
    if (_currentHand == nil) {
        _currentHand = [[NSMutableArray alloc]init];

    }
    self.currentHand = self.myDeck.giveHand;
    return _currentHand;

}

Any advice on this would be greatly appreciated, thanks in advanced.

  • I'm not sure the array will be necessary, but I could offer more specific advice if I knew a few more things about what you want. What do you want to happen after the first button is clicked at the beginning of the game? Secondly, how do you get the numbers to put in button titles? – rdelmar Jul 26 '12 at 17:34
  • @rdelmar I have it set up so that the user can select the number they want by clicking on it and pressing the enter button, when that happens it grabs the current title of the button and stores it as a variable. As for the numbers they come from an array called currentHand – William Louis Roach-Barrette Jul 26 '12 at 19:16
  • My question about the numbers was how do you choose the numbers that go into the hand? I'm not sure it's necessary to have an array at all, the numbers can just be stored as the titles of the 6 buttons, but that partly depends on how you choose those numbers. Are they randomly picked? Do you have multiple sets of 6 numbers stored somewhere from which you take one to populate the hand? – rdelmar Jul 26 '12 at 23:17
  • they are actually stored from a predefined array of numbers – William Louis Roach-Barrette Jul 27 '12 at 13:17

1 Answers1

0

Here is one way to approach this problem. As I said in my comments, I don't see any need for an array to keep track of the numbers as you click the button (just one to provide the original numbers for the title), or the need for an IBOutletCollection.

So, in the example below, I created a single view project, added 6 buttons to the view controller's view, set all their tags to 1 and connected them all up to the one action method, buttonPressed:. I don't use any outlets for the buttons, because you can do what you need to do just by looping through the view's subviews -- the tags aren't really necessary in this example, but if you had any other buttons in your view, the tags would be a way to distinguish these 6 game buttons from any others.

I'm not sure this is exactly the look you were going for, but try it out and see if it's close.

@interface ViewController : UIViewController

@property (assign) int total;
@property (retain) UIButton *previousButton;


#import "ViewController.h"

@implementation ViewController
@synthesize previousButton,total;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.total = 0;
    NSMutableArray *theData = [NSMutableArray arrayWithObjects:@"1",@"3",@"5",@"7",@"8",@"4",nil];
    for (id obj in [self.view subviews]) {
        if ([obj isKindOfClass:[UIButton class]] && [obj tag] == 1) {
            [(UIButton *)obj setTitle:theData.lastObject forState:UIControlStateNormal] ;
            [theData removeLastObject];
        }
    }
}

-(IBAction)buttonPressed:(UIButton *)sender {
    self.total += sender.titleLabel.text.intValue;
    [sender setTitle:[NSString stringWithFormat:@"%d",self.total] forState:UIControlStateNormal];
    self.previousButton.hidden = YES;
    sender.enabled = NO;
    [sender setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    self.previousButton = sender;

}

rdelmar
  • 103,982
  • 12
  • 207
  • 218