uipickerview selecting default row - ios

I am using a uipickerview to choose songs from. The problem is when I press play without interacting with the picker view (i.e trying to play the first song that is already selected/loaded by the picker view.) I get an error:
fatal error: unexpectedly found nil while unwrapping an Optional value
I have the code below set for didSelectRow:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
musicChoice = songs[row]
}
And as I understand, this only initiates once the user scrolls/ interacts with the picker view - so I am trying to use selectedRow somehow but am messing up, any ideas?
Updated pickerview code below:
var songs:[String] = []
var musicChoice = "Something"
let foods = ["Apples", "Bananas", "Corn", "Beans", "Tomatoes"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return songs[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return songs.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
musicChoice = songs[row]
}

The did select row method is self explanatory it says didSelectRow so you are right without interacting with the rows it doesn't initialise any thing.
Instead of selected row you can always initialise the first songs value to be played. i.e
musicChoice = songs[0]
while you declare the musicChoice variable. That would solve the purpose.

Related

I want to save a Selected Option in a Swift UIPickerView

so I have a UIPicker view. Now I have something selected now with that I want to save that selection so that the next time I go back to that page the same option that I selected is still there. right now it just goes to the first option selected which is grams.
Thx I'm new to Swift
Here is my code
#IBOutlet weak var pickerView: UIPickerView!
var measurementTypes = ["Grams","Ounces", "Pounds"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return measurementTypes.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return measurementTypes[row]
}
// Catpure the picker view selection
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selected = measurementTypes[row]
// This method is triggered whenever the user makes a change to the picker selection.
// The parameter named row and component represents what was selected.
}
You can simply save the selected row number in UserDefaults so that it can be accessed next time.
// Capture the picker view selection
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selected = measurementTypes[row]
// Save selected row integer in User Defaults
UserDefaults.standard.set(row, forKey: "pickerViewRow")
}
Then, in override func viewDidLoad() you can retrieve this value and set the picker view:
let row = UserDefaults.standard.integer(forKey: "pickerViewRow")
pickerView.selectRow(row, inComponent: 0, animated: False)

uipickerview index out if range

I'm trying to learn to code Swift and recently I faced a bug which is so embarrassing.
I have 2 textfield which both are connected to a picker view.
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if EntCity.isEditing
{
return MainCities[row].name
}
return MainJ[row].name
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if EntState.isFirstResponder
{
EntState.text = MainJ[row].name
Global_Addresses.global_address.cityID_fromState = MainJ[row].id
GetCities()
}
if EntCity.isEditing
{
if (EntState.text?.isEmpty)!
{
DispatchQueue.main.async {
self.DisplayMessage(UserMessage: "ابتدا استان را انتخاب کنید")
}
}
else
{
EntCity.text = MainCities[row].name
Global_Addresses.global_address.smallCityID = MainCities[row].id
}
}
EntState.text = MainJ[row].name
Global_Addresses.global_address.cityID_fromState = MainJ[row].id
GetCities()
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return MainCities.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return MainCities[row].name
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
EntCity.text = MainCities[row].name
Global_Addresses.global_address.smallCityID = MainCities[row].id
print("?????????????????????")
print(Global_Addresses.global_address.smallCityID)
}
}
the code works fine and both textfield can show the data in the picker view to choose. these textfield are connected, it means that
you have to choose one of the rows in textfield number 1 to get the data for textfield number 2 . when i choose the first data , and select the second textfield to choose the second one , it works fine untill i choose of those data which their row number is bigger than the first textfield
and as you can see i have declared that if it select first or second textfield to return different number for row!

UiPickerview values displayed as question mark ("?")in swift - But getting the correct value while assigning it to a textBox

//I am able to see the correct value in the text box upon scrolling down the picker view. But the picker view display is coming as "?" (question marks). I am confused with where I need to assign the pickerview as input view to the textbox . But still that won't make any sense to the error I am facing right .
I am using Xcode 9 and Swift 3.2.
class BillDetailsViewController: UIViewController ,UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource{
//Variables Declaration
//Picker view outlet
#IBOutlet var datePickerView: UIPickerView!
//Picker view datasource - integer array
var integerArray = [Int](1899...2500)
//Textbox where picked value to be shown
#IBOutlet var txtPaidYear: UITextField!
//ViewLoad
override func viewDidLoad() {
super.viewDidLoad()
self.hideKeyboardWhenTappedAround()
datePickerView.isHidden = true
datePickerView.reloadAllComponents()
datePickerView.dataSource = self
datePickerView.delegate = self
txtPaidYear.inputView = datePickerView
}
//picker view delegate functions
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return integerArray.count
}
public func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
let pickerValue: String = String(integerArray[row])
return pickerValue
}
public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
txtPaidYear.text = String(integerArray[row])
self.datePickerView.reloadAllComponents()
}
}
****You have to change method like this after that it will work definitely.**
and Delete Delegate for TextField which you are using..**
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
I just removed all the TextView delgates in the class and it worked . Still need to figure out what was the exact issue . One of the textView delegate method was causing the issue it seems . I need to figure it out.Anyway issue got fixed .I just created an independent project and implemented UIPIckerView for which it was working perfect. Hence I did the same in my project removing all the textview delegate methods .
Thanks much for all your support .
Replace:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int) -> String? {}
With:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {}

Update UITableViewCell

I've got a problem. I have this tableview and this pickerView
and this is the pickerView functions of the viewcontroller:
let gender = ["M","F"]
#IBOutlet weak var dataTableView: UITableView!
//pickerFuncs
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return gender[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return gender.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(gender[row])
}
How to modify the "m" in the cell with the selection of the uipicker? thanks everybody!
Create a variable
var currentGender = "M"
Set your TableViewCell with currentGender like
cell.gender = currentGender
When you select a row in UIPickerView,
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
currentGender = gender[row]
// Then reload your TableView row
let indexPath = IndexPath(item: rowNumber, section: 0)
tableView.reloadRows(at: [indexPath], with: .top)
}
I'm assuming you want to have your M and F appear inside your pickerView. To do this you have to create a reference. This can be done through an IBOutlet or you can do it programmatically like so: let pickerView = UIPickerView(). On a side note, I am also assuming you are only using one pickerView in the View Controller. If you are using multiple, set each of the pickerViews a tag. This can be done in the interface builder and programatically like so: pickerView.tag = 1. Then change your pickerView functions based on those tags.
Create a variable.
var gender = "M"
Initialise again your gender variable. In this delegate method.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
gender = gender[row]
// Then reload your TableView row
table view.reloadData()
}
Add these lines to your cellforrow_atIndexPath.
Cell.genderIdicatorLbl.text = gender
Note: genderIndicatorLbl is IBOutlet for your gender indicator Label

extract value from UIpickerview as variable

so i have this code for a UIpickerview and i want to get the value selected as a variable to use later, i tried using a variable gravity but it says "initialisation of variable gravity was never used...." here's my code:
var g = ["9.807", "3.711"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return g.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return g[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var gravity = Float(g[row])
}
You need to declare the var outside of the scope of your method:
var gravity: Float?
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
gravity = Float(g[row])
}
A local variable scope is limited to the method and as you don't use it within the method you get a warning for that.
This is Swift not Javascript :)
Your gravity var is local, so the given warning makes total sense (i.e., its scope is limited to the given function).
You should use the following UIPickerView method to get the currently selected index:
func selectedRow(inComponent component: Int) -> Int
For instance:
let gIndex = pickerView.selectedRow(inComponent: 0)
let gValue = Float(g[gIndex])!
Note that you do not even need to implement the didSelectRow delegate method to get this information.
As correctly pointed out in the comments, you of course need to declare and bind your UIPickerView to an instance var of your view controller:
#IBOutlet weak var pickerView: UIPickerView!
To use pickerView, first mention its DataSource and Delegate.
UIPickerViewDataSource, UIPickerViewDelegate
Now create this by right clicking on pickerView -> to the ViewContoller.swfit.You will get like:
#IBOutlet var pickerView: [UIPickerView]!
Create an array which contains the content you want to show in pickerView. Just like this:
var array:[String] = ["David","Mikel","Akhil","Akshay","Zlatan"]
Now you have to use pickerView functions. These functions can only be used if you mention DataSource and Delegate in class. The functions are:
// number of rows or number of components
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
// number of rows that we have to set
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return array.count
}
// set title in picker view according to the elements
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return array[row]
}
// when an element is selected, store in a string
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerString = array[row]
print(pickerString) // pickerString is String type. You can create this, to show the content selected.
}

Resources