UIPickerView selectRow doesn't works
While inside the viewDidLoad
method, your picker is empty. Since there is no 6th element to pick, it doesn't pick it.
You can do this if you delay execution of the select call until after the data is finished loading. Likely one cycle will be enough.
First though, try moving your select to viewDidAppear
.
Good luck!
UIPicker selectRow not working - what's wrong with this code?
It works if I put it in this method:
-(void) viewWillAppear: (BOOL) animated
UIPickerView selectRow not working as expected
One of the weird eccentricities of UIPickerView is that when you call
`[.. selectRow: inComponent: animated: ]`
this will trigger a call on the delegate's
[pickerView: didSelectRow: inComponent: ]
As a result of this it can be easy to create inadvertent race conditions, so check in your delegate's pickerView: didSelectRow: inComponent: method and be aware that this is being called in the middle here as a result of setting the first component. You might be better off storing a variable for what is selected in component0 and switching off that rather than off what is selected in the picker. You might also consider storing a boolean (listenToPicker) so you can switch it off temporally by putting the didSelect stuff inside a if (listenToPicker){ ..... }, then before setting it up with the selectRow: animated: calls you can set listenToPicker to NO, restoring it to YES after all components are set in viewDidAppear . Good luck
How to force UIPickerView.selectRow call didSelectRow method?
This to select the row
self.yourPickerViewName.selectRow(2, inComponent: 0, animated: true)
and this to trigger the method
self.pickerView(self.yourPickerViewName, didSelectRow: 2, inComponent: 0)
Combine them to simulate user action
How can I set selectRow on a UIPickerView that is attached to a UITextField
enum AvailableColors: Int {
case yellow = 0
case blue = 1
case red = 2
case green = 3
var colorByIndex: UIColor {
switch (self) {
case .yellow:
return .yellow
case .blue:
return .blue
case .red:
return .red
case .green:
return .green
}
}
}
class ViewController: UIViewController, UIPickerViewDelegate {
var picker : UIPickerView?
var selectedIndex = 0
override func viewDidLoad() {
super.viewDidLoad()
//1.init your picker with data.
// self.picker = UIPickerView()...
// self.picker.delegate = self
//2.select predefined index
self.picker?.selectRow(self.selectedIndex, inComponent: 0, animated: false)
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.selectedIndex = row
self.textField.backgroundColor = AvailableColors(rawValue: row)?.colorByIndex
}
}
Selecting a row in a UIPickerView - swift
It looks like you only have 3 elements in your picker. Pickerview element rows are 0-indexed, so instead of
pickerview.selectRow(1, inComponent: 0, animated: true)
pickerview.selectRow(2, inComponent: 0, animated: true)
pickerview.selectRow(3, inComponent: 0, animated: true)
you'll want
pickerview.selectRow(0, inComponent: 0, animated: true)
pickerview.selectRow(1, inComponent: 0, animated: true)
pickerview.selectRow(2, inComponent: 0, animated: true)
PickerVIew.selectRow doesn't work
Change your numberOfRowsInComponent
datasource method to the following:
// returns the # of rows in each component..
@available(iOS 2.0, *)
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
switch component {
case 0: return component1.count
case 1: return component2.count
case 2: return component3.count
default: return 0
}
}
You should also add a break
statement after your estAutoWin = true
line since you do not have to continue looping when you found a matching fruit.
estAutoWin = true
break
Related Topics
Missing Argument for Parameter 'From' in Call When Creating Instance of Codable Class
Safe to Signal Semaphore Before Deinitialization Just in Case
Swift: How to Implement a Login Storyboard
Cannot Add Alamofire to Swift Project
How Are Hash Collisions Handled
Cannot Convert Value of Type '()' to Specified Type Bool
Difference Between Dispatchsourcetimer, Timer and Asyncafter
How to Change the Z Index or Stack Order of Uiview
Gradient as Foreground Color of Text in Swiftui
Generatesdecimalnumbers for Numberformatter Does Not Work
How to Increase (Animate) the Width of the Square on Both Ends
Realitykit as a Framework to Build 3D Nonar Apps
Swift Switch Statement Considered All Cases of Int, But Compiler Still Display Error
How to Check If a Property Has Been Set Using Swift Reflection