Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the state of an UIButton in iOS using Swift?

I have Objective-C working code for six buttons but I don't seem to find how to change the state of a button using Swift. So how do I make the following code work in Swift?

[self.button2 setBackgroundImage:[UIImage imageNamed:@"button2_selected.png"] forState: UIControlStateSelected];
button1.selected = NO;
button2.selected = !button2.selected;
button3.selected = NO;
button4.selected = NO;
button5.selected = NO;
button6.selected = NO;
like image 343
eeschimosu Avatar asked Sep 20 '14 03:09

eeschimosu


1 Answers

Check for this:

    var var_button_ =   UIButton()
    var_button_.setBackgroundImage(UIImage(named: "button2_selected.png"), forState: UIControlState.Normal)
    var_button_.selected = false
    var_button_.selected = !var_button_.selected
    //var_button_.selected = true

Thanks BY JNYJ

like image 159
JNYJ Avatar answered Oct 21 '22 04:10

JNYJ