Pregunta

I'm not sure if this has been asked before, but I'm gonna ask anyways. I am working on a grid of buttons, with letters as the titles of each one. I am getting the contents of these letters from an array, and I believe that is where my issue is coming from. The titles appear just fine, but when I press on any of the buttons in my array, that is where my issue comes in.

Every time I press a letter, the part of my NSLog NSLog(@"Letter Added to Array: %@", self.sectionButton.titleLabel.text); displays

Letter Added to Array: S

and that is all that is displayed. No matter what button is pushed. I am thinking it might just be because of S being the last object in my array, which is why it's saying that. I don't know, so ANY help would be appreciated.

Here is my code:

- (void) didTouchButton
{
[self.lettersPressedArray addObject:self.sectionButton.titleLabel.text];
NSLog(@"Letter Added to Array: %@", self.sectionButton.titleLabel.text);
NSLog(@"Letters Pressed = %@", self.lettersPressedArray);
}

- (void)showGridWithRows:(int)r columns:(int)c arrayOfContent:(NSArray *)content withSizeOfContent:(CGFloat)contentSize
{
for (int i = 0; i < content.count; i++) {

    // vars
    int row = (int)i / r; // to figure out the rows
    int col = i % c;      // to figure out the columns
    float x = 0;
    float y = 0;

    // sizing options
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        CGSize screen = [UIScreen mainScreen].bounds.size;

        if (screen.height == 480) {
            x = 1 + (40 * col);
            y = 1 + (30 * row);
        }
        else {
            x = 2 + (40 * col);
            y = 1 + (31 * row);
        }
    }
    else {
        x = .5 + (90 * col);
        y = 1 + (90 * row);
    }

    //button
    self.sectionButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.sectionButton setTintColor:[UIColor lightGrayColor]];
    self.sectionButton.frame = CGRectMake(x, y, contentSize, contentSize);
    self.sectionButton.tag = 100 + row * c + col;
    [self.sectionButton addTarget:self action:@selector(didTouchButton) forControlEvents:UIControlEventTouchUpInside];

    // font stuff
    self.sectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.sectionButton.titleLabel.backgroundColor = [UIColor clearColor];
    self.sectionButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:22];
    [self.sectionButton setTitleColor:[UIColor blackColor]/*[UIColor colorWithRed:241.0/255.0 green:124.0/255.0 blue:22.0/255.0 alpha:1.0]*/ forState:UIControlStateNormal];

    // title
    s = (NSString *)[content objectAtIndex:i];
    [self.sectionButton setTitle:s forState:UIControlStateNormal];

    // color
    if ([s isEqualToString:@"A"] ) {
        [self.sectionButton setBackgroundColor:[UIColor greenColor]];
    }
    else if([s isEqualToString:@"Z"]) {
        [self.sectionButton setBackgroundColor:[UIColor redColor]];
    }
    else {
        [self.sectionButton setBackgroundColor:[UIColor lightGrayColor]];
    }

    //layer
    self.sectionButton.layer.borderWidth = .65;
    //self.sectionButton.layer.cornerRadius = 5.0;

    for(int j = 0; j < c; j++) {
        for (int k = 0; k < r; k++) {
            [self addSubview:self.sectionButton];
        }
    }
}

}

¿Fue útil?

Solución 2

Couple things here. Firstly, you likely don't want to have the button be a property, because you are constantly overwriting it and are ultimately left with it referencing the last one you created... What you're basically doing is the same as:

int x = 1; x = 2; x = 3;

printing x will ALWAYS result in 3... Make sense?

The solution to your problem is to pass the button you are tapping as a parameter to the function that handles the action, by adding in a ":" after "didTouchButton" and changing the way you create that function. When you create the button, add the : after the function name like this:

[button addTarget:self action:@selector(didTouchButton:)  forControlEvents:UIControlEventTouchUpInside];

That allows a reference to the button pressed to be passed to the function, so you can do this to handle it:

- (void)didTouchButton:(UIButton *)button {
    NSString *titleOfPressedButton = button.titleLabel.text;
}

Otros consejos

Change this:

- (void) didTouchButton
{
[self.lettersPressedArray addObject:self.sectionButton.titleLabel.text];
NSLog(@"Letter Added to Array: %@", self.sectionButton.titleLabel.text);
NSLog(@"Letters Pressed = %@", self.lettersPressedArray);
}

To this:

- (void) didTouchButton:(UIButton *)sender
{
[self.lettersPressedArray addObject:sender.titleLabel.text];
NSLog(@"Letter Added to Array: %@", sender.titleLabel.text);
NSLog(@"Letters Pressed = %@", self.lettersPressedArray);
}

And when you assign your button selector add a ':'

So, this:

@selector(didTouchButton)

Will be:

@selector(didTouchButton:)

Probably implemented like:

[someButton addTarget:self action:@selector(didTouchButton:) forControlEvents:UIControlEventTouchUpInside];

Because:

The way you originally reference your button via self.selectionButton.titleLabel.text accesses one specific instance of a button. This means that no matter what button triggers didTouchButton it gets the content of self.selectionButton which I'm assuming displays an "S". This means that no matter what happens, you add more of the letter "S" to your array. By configuring our action method like we did, it will pass "self" as an argument. This means that we have a variable representing whoever called the method within the method. We will use that to get our contents and add them to the array.

You need to change didTouchButton method to accept (id)sender parameter and change how you set up it's selector while creating a button to didTouchButton:.

This way you will receive a button object pointer inside the didTouchButton and will be able to get its information.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top