Search code examples
objective-cios7xcode5

When clicking first button,it becomes highlighted and while clicking second,second highlighted and first become normal


I am creating an app,which contains five buttons ,created programmatically.The following are the requirements...

while clicking the first button it stay highlighted.while clicking second button ,first become normal and second stays highlighted...ie the particular button clicked becomes highlighted ,all others remains normal.......please help..


Solution

  • You can set

    [button setBackgroundImage:[UIImage imageNamed:@"normalbackgroundimage"] forState:UIControlStateNormal]; //not highlighted
    [button setBackgroundImage:[UIImage imageNamed:@"highlightedbackgroundimage"] forState:UIControlStateHighlighted | UIControlStateSelected]; //highlighted
    

    and now when you set your button.selected = YES it will get highlighted

    so you could do something like this on button click

    button1.selected = YES;
    otherButton.selected = NO;
    

    EDIT

    Simple solution to your problem would be to create one ibaction connect it to all your buttons you want to selected/deselect and just do this

    -(IBAction)someButtonPressed:(UIButton*)sender{
        button1.selected = NO;
        button2.selected = NO;
        button3.selected = NO; //and so on...just set all your buttons to selected = NO
    
        //at the end you just select button you clicked
        sender.selected = YES;
    }