ios - How do I change the background color of the text in view of the UIPickerView with multiple components when a valid selection is made, in Swift -
below code change background color of uipickerview cyan showing valid selection when total of row indexes = 6. have pickerlabel background in view(selected) turn cyan instead in 4 components when selection valid. idea?
import uikit class viewcontroller: uiviewcontroller, uipickerviewdatasource, uipickerviewdelegate { @iboutlet weak var mypicker: uipickerview! let pickerdata0 = ["0r","1r","2r","3r","4r","5r","6r"] let pickerdata1 = ["0b","1b","2b","3b","4b","5b","6b"] let pickerdata2 = ["0y","1y","2y","3y","4y","5y","6y"] let pickerdata3 = ["0g","1g","2g","3g","4g","5g","6g"] override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. mypicker.datasource = self mypicker.delegate = self } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } //mark: delegates , data sources //mark: data sources func numberofcomponentsinpickerview(pickerview: uipickerview) -> int { return 4 } func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int { return pickerdata0.count } //mark: delegates func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) { let redindex = mypicker.selectedrowincomponent(0) let blueindex = mypicker.selectedrowincomponent(1) let yellowindex = mypicker.selectedrowincomponent(2) let greenindex = mypicker.selectedrowincomponent(3) if redindex + blueindex + yellowindex + greenindex == 6 { mypicker.backgroundcolor = uicolor.cyancolor() } else { mypicker.backgroundcolor = uicolor.whitecolor() } } func pickerview(pickerview: uipickerview, viewforrow row: int, forcomponent component: int, reusingview view: uiview!) -> uiview { switch component { case 0: var pickerlabel = view as! uilabel! if view == nil { pickerlabel = uilabel() pickerlabel.textcolor = uicolor.redcolor() pickerlabel.backgroundcolor = uicolor.graycolor() pickerlabel.textalignment = nstextalignment.center pickerlabel.text = pickerdata0[row] pickerlabel.font = uifont(name: "georgia", size: 26) } return pickerlabel case 1: var pickerlabel = view as! uilabel! if view == nil { pickerlabel = uilabel() pickerlabel.textcolor = uicolor.bluecolor() pickerlabel.backgroundcolor = uicolor.graycolor() pickerlabel.textalignment = nstextalignment.center pickerlabel.text = pickerdata1[row] pickerlabel.font = uifont(name: "georgia", size: 26) } return pickerlabel case 2: var pickerlabel = view as! uilabel! if view == nil { pickerlabel = uilabel() pickerlabel.textcolor = uicolor.yellowcolor() pickerlabel.backgroundcolor = uicolor.graycolor() pickerlabel.textalignment = nstextalignment.center pickerlabel.text = pickerdata2[row] pickerlabel.font = uifont(name: "georgia", size: 26) } return pickerlabel case 3: var pickerlabel = view as! uilabel! if view == nil { pickerlabel = uilabel() pickerlabel.textcolor = uicolor.greencolor() pickerlabel.backgroundcolor = uicolor.graycolor() pickerlabel.textalignment = nstextalignment.center pickerlabel.text = pickerdata3[row] pickerlabel.font = uifont(name: "georgia", size: 26) } return pickerlabel default: return pickerview } } }
u want pickerlabel background in view(selected) turn cyan instead in 4 components. u can change code in pickviewdidselectrow :
if redindex + blueindex + yellowindex + greenindex == 6 { mypicker.backgroundcolor = uicolor.cyancolor() } else { mypicker.backgroundcolor = uicolor.whitecolor() }
to :
if redindex + blueindex + yellowindex + greenindex == 6 { mypicker.viewforrow(row, forcomponent: component)?.backgroundcolor = uicolor.cyancolor() } else { mypicker.viewforrow(row, forcomponent: component)?.backgroundcolor = uicolor.whitecolor() }
btw i'm not sure whether fit needs.
Comments
Post a Comment