Question

I have an IBOutletCollection of UIButtons:

@property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *Buttons;

with an ibaction i would to change the highlighted state permanently after the touch down event.

This Problem is very similar to this: IBOutletCollection of UIButtons - changing selected state of buttons

... but with the for-loop the buttons doesnt change.

i also tried the perfomselector method from here: Keep iPhone UIButton Highlighted

but it doesnt work.

now my code:

-(IBAction)toggleButtons:(id)sender
{
    NSUInteger Index = [button tag];
    [[Buttons objectAtIndex:Index] setHighlighted:YES];
}

if i change line four to this:

    [[Buttons objectAtIndex:3] setHighlighted:YES];

it works for the fourth element in my collection... But not with the index variable....

regards, phil

Update

SelectionViewController.h

#import <UIKit/UIKit.h>

@interface SelectionViewController : UIViewController

@property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *Buttons;

- (IBAction)toggleButtons:(id)sender;

@end

SelectionViewController.m

#import "SelectionViewController.h"

@interface SelectionViewController ()

@end

@implementation SelectionViewController

@synthesize Buttons;


-(IBAction)toggleButtons:(id)sender
{
    UIButton *button = sender;
    NSUInteger Index = [button tag];
    [self performSelector:@selector(doHighlight:) withObject:sender afterDelay:0];

    [[Buttons objectAtIndex:Index] setHighlighted:YES];
}
- (void)doHighlight:(UIButton *)b {
    [b setHighlighted:YES];
}

XIB File

Okey Update 2:

Now i had declared my Buttons as normal IBOutlet and this is not working:

-(IBAction)toggleButtons:(id)sender
{
    UIButton *button = sender;

    [button setHighlighted:YES];
}

But if change it to this:

-(IBAction)toggleButtons:(id)sender
{    
    [myOutletButton setHighlighted:YES]; //Normal Outlet 
}

it works....

But why is not possible with the sender?

regards!

Update 3

This works also:

for(id button in self.view.subviews)
{
    [button setHighlighted:YES];

}

Ok if change the delay time in the selector to 1, the state will be highlighted. I am using "touch down" event... i think after i touched up the button gets its old state. Which event is the right?

Was it helpful?

Solution

Given that your example works with a specific integer, the problem is probably that the tag property is not set properly for each of your buttons. If the buttons are created in interface builder, each of them will have a default tag value of 0. To check this, click on the button and then, in the Attributes Inspector, scroll down to View and see what value is entered in the tag field

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top