user1626438
user1626438

Reputation: 133

Button setHighlighted not working

I have a button which I want to highlight using the highlighted attribute. In interface builder, the highlighted attribute works and changes the button to a visibly highlighted state. However, in code, when I write [myButton setHighlighted:YES]; nothing changes. Any ideas as to why this doesn't work?

Upvotes: 0

Views: 3477

Answers (5)

Hitesh
Hitesh

Reputation: 552

I have myself encountered such a trouble when i needed to keep a button highlighted for sometime.

Make sure, the target that you have added to the button is of IBAction type. Lemme show you, with the help of a snippet :

@property (strong, nonatmoic) UIButton *myBtn;

IN .m file,

self.myBtn = [[UIButton alloc]init];
self.myBtn = [UIButton buttonWithType : UIButtonTypeCustom];

[self.myBtn addTarget:self selector:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];

And then while declaring the target method

-(IBAction)btnPressed: (id)sender
{
   UIButton *btnObj = (UIButton*).sender;
   [btnObj setHighlighted:true];
}

Doing this, will definately solve your problem, as the button that is pressed, will respond to the IBAction and sender will help in defining the button that responds to this method.

Upvotes: 1

Jack
Jack

Reputation: 81

UIButton have 4states: Normal, Highlighted, Selected and Disable.

-If UIButton is enable, highlighted will active when you touch on it. And it will change back to Normal state when release. Also the command [button setHighlighted:YES]; just make it change to highlighted state in a very small time and auto back to Normal. That's reason you can't keep button highlighted.

-To keep it, you must use 2 method of @Vineet Singh answer. But you need to setHighlighted:NO to bring it back to Normal state.

You can use [button setSelected:YES/NO]; to control but you need to setImageForState, or setTitleForState same as Highlighted state.

Upvotes: 0

Durican Radu
Durican Radu

Reputation: 1337

Please remember that the correct behaviour is the next: The highlighted state for a button is only when the button is tapped, so changing the button to this state isn't exactly correct. There is another control state which is appropriate, selectedState

If you modify the selected state in the IBAction of that button it will work. Hacking the highlighted state of the button in the moment you tap on it, it's wrong.

-(IBAction)touch:(UIButton *) tappedButton {
    [myButton setSelected:YES]
}

You only need to provide the resources for the selectedState. You can do that via xib ( choose the state for the button in the attributes inspector for selected and you can add textColor background etc.) or via code:

 [self.button setImage:[UIImage imageNamed:image] forState:UIControlStateSelected];

Upvotes: 4

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

Button is highlighted when it is selected.So try calling [button setSelected:YES]; instead Of [button setHighlighted:YES]; May this help u.

Upvotes: 0

Vineet Singh
Vineet Singh

Reputation: 4019

You can solve this problem by doing [button setHighlighted:YES] in the next runloop:

- (void)highlightButton:(UIButton *)b { 
    [b setHighlighted:YES];
}

 - (IBAction)onTouchup:(UIButton *)sender {
    [self performSelector:@selector(highlightButton:) withObject:sender afterDelay:0.0];
}

And if it doesn't work,you can try this alternate method:

-(void)onTouchup:(UIButton*) button
{
    [NSOperationQueue.mainQueue addOperationWithBlock:^{ button.highlighted = YES; }];
}

Upvotes: 1

Related Questions