Tarayaa
Tarayaa

Reputation: 127

If statement on UIButton selection

Im not sure why this is not working:

- (IBAction)sliderValueChanged:(UISlider *)sender {
    if (_menuRightItem2.selected == YES) {
        NSLog(@"yes");
        if (sender == self.slider1)
            [sender setValue:((int)((slider1.value + 1) / 1.0) - 1.0) animated:NO];
        else if (sender == self.slider2)
            [sender setValue:((int)((slider2.value + 1) / 1.0) - 1.0) animated:NO];
    }
    barHeight = slider1.value * -20 + slider2.value * -50; //! hoogte in de min, yea right.
    [UIView animateWithDuration:0.50 animations:^{
        self.orangeView.frame = CGRectMake(595, 615, 80, barHeight);
    }];
}

What I'm trying to accomplish is that when menuRightItem2 is selected that it then executes my if else statement and otherwise does not. But my NSLog() isn't even working. What would be the correct way to achieve this?

My button statement:

- (IBAction)menuRightItem2Touch:(id)sender {
    _menuRightItem2.selected = !_menuRightItem2.selected;
}

Upvotes: 0

Views: 641

Answers (3)

ajithmn89
ajithmn89

Reputation: 249

check whether the button is selected, if not use the setselected function as shown below

- (IBAction)menuRightItem2Touch:(id)sender {    
    if([_menuRightItem2 isSelected])
    {        
        [_menuRightItem2 setSelected:NO];        
    }    
    else{
        [_menuRightItem2 setSelected:YES];
    }
}

Upvotes: 0

Puneet Sharma
Puneet Sharma

Reputation: 9484

Well , I tried to implement your code on basis of this understanding: _menuRightItem2 is UIButton and is listening for UIControlEventTouchUpInside events using

-(IBAction)menuRightItem2Touch:(id)sender method.

There are two sliders (IBOutlets) slider 1 , slider2. Both listen for ValueChanged Events using

-(IBAction) sliderValueChanged:(UISlider*)sender

The code seems to run perfectly. Please make sure you have made _menuRightItem2 listen to -

-(IBAction)menuRightItem2Touch:(id)sender

Upvotes: 0

Bonnie
Bonnie

Reputation: 4953

[_menuRightItem2 isSelected] returns a bool

you can directly use this method in the if statement, which is the recommended way to do it, rather than equating it to YES

so try and use

if([_menuRightItem2 isSelected]){

}

hope it helps.

Upvotes: 1

Related Questions