ios - how to change the button colour if checkbox is checked in swift? -
i have agree button below 2 checkbox. want enable agree button , change colour of agree button after both checkbox checked. have used custom images checkbox. using code
this uibutton class
let selectedimage = uiimage(named: "checkbox_checked") let unselectedimage = uiimage(named: "checkbox_unchecked") class qbutton: uibutton { //bool property var ischecked:bool = false{ didset{ if ischecked { self.setimage(selectedimage, forstate: uicontrolstate.normal) }else{ self.setimage(unselectedimage, forstate: uicontrolstate.normal) } } } override init(frame: cgrect){ super.init(frame:frame) self.layer.maskstobounds = true self.setimage(unselectedimage, forstate: uicontrolstate.normal) self.addtarget(self, action: "buttonclicked:", forcontrolevents: uicontrolevents.touchupinside) self.ischecked = false } required init(coder adecoder: nscoder) { super.init(coder: adecoder)! } func buttonclicked(sender: uibutton) { if(sender == self){ if ischecked == true{ ischecked = false print("true") self.setimage(unselectedimage, forstate: uicontrolstate.normal) }else{ ischecked = true print("false") self.setimage(selectedimage, forstate: uicontrolstate.selected) } nsuserdefaults.standarduserdefaults().setobject(ischecked, forkey: "isbtnchecked") nsuserdefaults.standarduserdefaults().synchronize() } } }
and view controller class using qbutton class
checkboxone = qbutton(frame: cgrectmake(x,y,width,height)) ischecked = nsuserdefaults.standarduserdefaults().boolforkey("isbtnchecked") checkboxone.ischecked = ischecked self.view.addsubview(checkboxone) checkboxtwo = qbutton(frame: cgrectmake(x,y,width,height)) checkboxtwo.ischecked = ischecked self.view.addsubview(checkboxtwo)
now want change color of agree button if user check both checkboxes.
there problem logic of qbutton. storing checked state in user defaults property isbtnchecked
. there multiple instances of qbutton (2 checkboxes), 1 checked property set true. avoid storing state of checkbox in defaults , instead query state checkboxone , checkboxtwo using ischecked
property.
Comments
Post a Comment