0

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?

Community
  • 1
  • 1
Phil
  • 341
  • 6
  • 19

1 Answers1

0

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

John Dalton
  • 150
  • 7
  • hey john, the tag property is set properly. I printed it to console and it was correct. could it be the an allocation problem of the uibuttons? should i allocate the buttons especially? – Phil Sep 08 '12 at 17:48
  • You should check that each button has a link to the Buttons IBOutletCollection. In Interface Builder, right click on each button and make sure that Buttons is listed under Referencing Outlet Collections. If it is not, control-drag from it to the IBOutlet in your ViewController header file. – John Dalton Sep 08 '12 at 18:10
  • Can't really do any more without seeing all the code. Happy to take a look if you can paste a link – John Dalton Sep 09 '12 at 10:02
  • Ok i had tested it with with normal IBOutlets and having the same problem. – Phil Sep 10 '12 at 08:39
  • 1
    You should use the "Touch Up Inside" event. This is the default for buttons. I tested your code using this and it seems to work OK. (By the way, you don't need the call to **[[Buttons objectAtIndex ... ** if you are using the **PerformSelector** call) – John Dalton Sep 11 '12 at 08:16
  • 1
    FYI ioS automatically highlights button on touch (try it on a button with no actions attached). If you use the _Touch Down_ event, your message is sent, but then the button is unhighlighted by default on the _Touch Up_ event. If you use the _Touch Up Inside_ event, your message is sent after the system message, and so has the desired effect. – John Dalton Sep 11 '12 at 08:35