maddy
maddy

Reputation: 4111

deselecting other buttons on click of any one uibutton-iphone

how to deselect another two button on clicking of one button.i am able to change the image of button by clicking them.I have created three buttons from IB.and their ibaction is as follow:

- (IBAction)todaybuttonClicked:(id)sender
{
    todayButton.tag=0;

    NSLog(@"hi todaybuttonClicked");
    if ([sender isSelected]) {
        [todayButton setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];

        [sender setSelected:NO];
    }
    else{
        [sender setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
        [sender setSelected:YES];
    }  
        [self todayOffersSegmentSelected];

}

- (IBAction)tomorrowbuttonClicked:(id)sender
{
    tomorrowButton.tag=1;

    NSLog(@"hi tomorrowbuttonClicked");

    if ([sender isSelected]) {

        [tomorrowButton setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];

        [sender setSelected:NO];
    }
    else{
        [sender setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];
        [sender setSelected:YES];

    }
    [self tomorrowOffersSegmentSelected];
}

- (IBAction)restoftheweekbuttonClicked:(id)sender
{
    restoftheweekButton.tag=2;

    NSLog(@"hi restoftheweekbuttonClicked");

    if ([sender isSelected])
    {
        [restoftheweekButton setBackgroundImage:[UIImage imageNamed:@"normal.png"] forState:UIControlStateNormal];

        [sender setSelected:NO];

    }

    else{

        [sender setBackgroundImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateSelected];

        [sender setSelected:YES];

    }
    [self restOfWeekOffersSegmentSelected];

}

any suggestion?? Thanks

Upvotes: 0

Views: 2660

Answers (3)

Hrvoje Stanisic
Hrvoje Stanisic

Reputation: 2295

In your viewDidLoad you can initialize _days array with your buttons:

_daysArray =[[[NSArray alloc]initWithObjects:self.sunday,self.monday,self.tuesday,self.wednesday,self.thursday,self.friday,self.saturday nil]retain];

If you're using IB to create the buttons, just point all of them at the same IBAction in your class:

-(IBAction)changeSelectedDay:(id)sender
{
    for (UIButton *button in _buttonsArray) {
        [button setSelected:([button isEqual:sender])?YES:NO];
    }
}

IMHO, this approach is much cleaner.

Upvotes: 5

Praveen S
Praveen S

Reputation: 10393

You can use IBOutletCollection. Its for similar requirements. I will try to paste a blog post which explained it in detail.

Some related SO posts are: Practical efficient usage of IBOutletColletion

Upvotes: 0

Man of One Way
Man of One Way

Reputation: 3980

When pressing today button you could deselect the other two buttons the following way:

-(IBAction)todaybuttonClicked:(id)sender
{

    [tomorrowButton setSelected: NO];
    [restoftheweekButton setSelected: NO];


    //... do rest of stuff
}

Use the same pattern when selecting another button.

Upvotes: 1

Related Questions