ios - UIPickerView showing empty grey box Swift -
i haven't whole lot of code, might copy here.
class viewcontroller: uiviewcontroller, uipickerviewdelegate, uipickerviewdatasource{ var buildings = ["bankbuilding", "cinema" , "cornershop", "greg's house"] @iboutlet weak var buildtext: uitextfield! var buildpickers:uipickerview = uipickerview() override func viewdidload() { super.viewdidload() buildpickers = uipickerview() buildpickers.delegate = self buildpickers.hidden = true; buildtext.inputview = buildpickers buildtext.text = buildings[0] } func numberofcomponentsinpickerview(pickerview: uipickerview) -> int{ return 1 } func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int{ println("count: \(buildings.count)") return buildings.count } func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string! { println("creating title: \(buildings[row])") return buildings[row] } func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) { println("selected: \(buildings[row])") buildtext.text = buildings[row] buildpickers.hidden = true; } func textfieldshouldbeginediting(textfield: uitextfield) -> bool { buildpickers.hidden = false return false } }
the print statements correct. numberofrows...
, titleforrow
printing correct strings.
but there no prints didselectrow
because, well, can't select row.
this get:
you can ignore google map in background, shouldn't interfere picker view , set in storyboard.
the grey window appears when click on textfield never shows content. print statements otherwise.
does know why case?
just add line in viewdidload
method:
buildpickers.datasource = self
and code be:
override func viewdidload() { super.viewdidload() buildpickers = uipickerview() buildpickers.delegate = self buildpickers.datasource = self buildpickers.hidden = true; buildtext.inputview = buildpickers buildtext.text = buildings[0] }
and show data.
update:
it not showing because set hidden in viewdidload
.
just remove line code:
buildpickers.hidden = true
here working code:
import uikit class viewcontroller: uiviewcontroller, uipickerviewdelegate, uipickerviewdatasource, uitextfielddelegate{ var buildings = ["bankbuilding", "cinema" , "cornershop", "greg's house"] @iboutlet weak var buildtext: uitextfield! var buildpickers:uipickerview = uipickerview() override func viewdidload() { super.viewdidload() buildpickers = uipickerview() buildpickers.delegate = self buildpickers.hidden = true buildtext.delegate = self //set delegate textfield buildtext.inputview = buildpickers buildtext.text = buildings[0] } func numberofcomponentsinpickerview(pickerview: uipickerview) -> int{ return 1 } func pickerview(pickerview: uipickerview, numberofrowsincomponent component: int) -> int{ println("count: \(buildings.count)") return buildings.count } func pickerview(pickerview: uipickerview, titleforrow row: int, forcomponent component: int) -> string! { println("creating title: \(buildings[row])") return buildings[row] } func pickerview(pickerview: uipickerview, didselectrow row: int, incomponent component: int) { println("selected: \(buildings[row])") buildtext.text = buildings[row] buildpickers.hidden = true; } //this method call when click on textfield func textfieldshouldbeginediting(textfield: uitextfield) -> bool { buildpickers.hidden = false return true } }
Comments
Post a Comment