I was searching very much, but i did not find an answer for my question.
My app should change the data of one picker after the other picker was changed.
As you can see, in the titleForRow method, I want to set the data and I also know that this method is just called once, but I just do not know where and also how I should do it then.
I think, on the didSelectRow method I could set the data, too, but there I do not have a gleam how I should do it.
Here is my code, I hope this is helpful.
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var x = 0
#IBOutlet weak var leaguePicker: UIPickerView!
#IBOutlet weak var clubPicker: UIPickerView!
let league = ["Bundesliga", "Premier League"]
let club = [["ManCity", "Arsenal"],["Bayern", "Dortmund"]]
override func viewDidLoad() {
super.viewDidLoad()
leaguePicker.delegate = self
leaguePicker.dataSource = self
clubPicker.delegate = self
clubPicker.dataSource = self
}
func numberOfComponents(in picker: UIPickerView) -> Int {
return 1
}
func pickerView(_ picker: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
switch (picker){
case leaguePicker:
return league.count
case clubPicker:
return club.count
default:
return 1
}
}
func pickerView(_ picker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch (picker){
case leaguePicker:
return league[row]
case clubPicker:
if (x==0){
print("titleForRow call.. x=0")
return club[0][row]
// this is just happening at the beginning
} else {
print("titleForRow call.. x=1")
return club[1][row]
// this does not happen
}
default: return "an error occurred"
}
}
func pickerView(_ picker: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if (picker == leaguePicker) {
if (row==0){
//clubPicker.selectRow(0, inComponent: 0, animated: true)
x=0
} else {
x=1 // this call is working
//clubPicker.selectRow(0, inComponent: 1, animated: true)
}
}
picker.reloadAllComponents()
//this does not lead to call titleForRow
}
}
You need to reload the 2nd picker when the value in the 1st picker changes.
func pickerView(_ picker: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if (picker == leaguePicker) {
x = row
clubPicker.reloadAllComponents()
}
}
You also need to fix the numberOfRowsInComponent:
func pickerView(_ picker: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
switch (picker){
case leaguePicker:
return league.count
case clubPicker:
return club[x].count
default:
return 1
}
}
And your titleForRow can be simplified:
func pickerView(_ picker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch (picker){
case leaguePicker:
return league[row]
case clubPicker:
return club[x][row]
default: return "an error occurred"
}
}
I also suggest you renamed your x variable to something more useful such as selectedLeague.
Related
I am trying to develop a function in which the user enters a number in a TextField and based on the selection of the picker, it will calculate the number and display it either in a label or another text field. Now keep in mind, the picker has to retain String titles, but it will have different numbers for each row.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate,
UIPickerViewDataSource {
#IBOutlet weak var numberInput: UITextField!
#IBOutlet weak var pickerControl: UIPickerView!
#IBOutlet weak var pickerControl2: UIPickerView!
#IBOutlet weak var resultField: UILabel!
var pickerData = ["Logistic","Administration","Clinic"]
var pickerData2 = ["Days","Weeks","Months"]
override func viewDidLoad() {
super.viewDidLoad()
pickerControl.delegate = self
pickerControl2.delegate = self
pickerControl.delegate = self
pickerControl2.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView,
numberOfRowsInComponent component: Int) -> Int {
if pickerView == pickerControl {
return pickerData.count
} else if pickerView == pickerControl2 {
return pickerData2.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row:
Int, forComponent component: Int) -> String? {
if pickerView == pickerControl {
return pickerData[row]
} else if pickerView == pickerControl2 {
return pickerData2[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row:
Int, inComponent component: Int) -> String? {
let entry = Float(numberInput.text!) ?? 0.0
if pickerView == pickerControl {
return (Array(pickerData)[row])
}
if pickerView == pickerControl2 {
if Array(pickerData2)[row] == "Days" {
self.resultField.text = String(entry * 3)
}
if Array(pickerData2)[row] == "Weeks" {
self.resultField.text = String(entry * 4)
}
if Array(pickerData2)[row] == "Months" {
self.resultField.text = String(entry * 6)
}
return Array(pickerData2)[row]
}
return ""
}
}
The function signature for pickerView(didSelect...) isn't correct, so it's not getting called. You'll see that Xcode is giving you a warning about it. That function shouldn't be returning anything.
Here's an example of how to change it:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let entry = Float(numberInput.text ?? "") ?? 0.0
if pickerView == pickerControl {
return
}
switch pickerData2[row] {
case "Days":
self.resultField.text = String(entry * 3)
case "Weeks":
self.resultField.text = String(entry * 4)
case "Months":
self.resultField.text = String(entry * 6)
default:
break
}
}
You'll note that I also used a switch statement, which I think is a little cleaner looking. But, if you didn't and stuck to your if/else, you don't have to use Array() for everything -- you can do things like if pickerControl2[row] == "Days" { }
You could clean things up even further by defining pickerData2 as:
var pickerData2 : [(title: String, value: Float)] = [(title: "Days",value: 3),(title: "Weeks",value: 4),(title: "Months", value:6)]
Then, in titleForRow, you could return pickerData2[row].title
and later, in didSelectRow:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let entry = Float(numberInput.text ?? "") ?? 0.0
if pickerView == pickerControl {
return
}
self.resultField.text = String(entry * pickerData2[row].value)
}
I have a UIPickerView that when tapping the done button, keeps selecting the last option of the Array. I can tap on either the UITextField or a button I'm using that assigns the UITextField as the first responder. My code is as follows:
Edit. I should also mention that I'm using Xcode 11 GM Seed if that was to make a difference. I tested a clean version of the code shown below on a separate project with nothing else and it is displaying the same behavior.
#IBOutlet weak var sortTypeTextField: UITextField!
var selectedSortOption = String()
let sortOptions = [
USER_REVIEWS_SORT_OPTION_DATE,
USER_REVIEWS_SORT_OPTION_LIKES,
USER_REVIEWS_SORT_OPTION_DISLIKES
]
sortOptions above is an array of Strings located in a different file but for the purpose of this question I’ll add here:
let USER_REVIEWS_SORT_OPTION_DATE = "Date"
let USER_REVIEWS_SORT_OPTION_LIKES = "Number of Likes"
let USER_REVIEWS_SORT_OPTION_DISLIKES = "Number of Dislikes"
let DONE_TOOLBAR_BUTTON_TITLE = "Done"
I have a function setupUI which I call in viewDidLoad along with the value for the sortTypeTextField. The value is the first String in the array when the viewController first loads.
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
}
func setupUI() {
sortTypeTextField.text = sortOptions[0]
createSortPicker()
createPickerToolbar()
}
func createSortPicker() {
let sortPicker = UIPickerView()
sortPicker.delegate = self
sortTypeTextField.inputView = sortPicker
}
func createPickerToolbar() {
let toolBar = UIToolbar()
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: DONE_TOOLBAR_BUTTON_TITLE, style: .plain, target: self, action: #selector(dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
sortTypeTextField.inputAccessoryView = toolBar
}
#objc override func dismissKeyboard() {
view.endEditing(true)
}
I’m also using a button to display the UIPicker as shown here:
#IBAction func sortRatingsButtonWasTapped(_ sender: Any) {
sortTypeTextField?.becomeFirstResponder()
}
As shown below, I want for the sortTypeTextField value to change to the selected option but for some reason when tapping the Done button in the toolbar, the text shown is always the third String in the array regardless if I have chosen the first, the second or the third.
extension ReviewsViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
sortOptions.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return sortOptions[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedSortOption = sortOptions[row]
sortTypeTextField.text = selectedSortOption
}
}
It seems to be a bug in the simulator. I tested the my code above and the code provided by Abdul Karim Khan on my device and the work as they should. The label updates according to the option selected in the picker view.
This is how I created pickerView,
class ViewController: UIViewController {
#IBOutlet weak var selectaday: UITextField!
let days = ["monday","wednesday"]
var selectedDay: String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
createDayPicker()
}
func createDayPicker(){
let dayPicker = UIPickerView()
dayPicker.delegate = self
selectaday.inputView = dayPicker
}
}
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource{
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return days.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return days[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedDay = days[row]
selectaday.text = selectedDay
}
}
I am trying to set up 2 UIPickerViews in 1 view controller. I have not been able to find resources that solve my problem.
I have tried following the answer from Diavel Rider on How to use 2 UIPickerView's in one View Controller?. I got some errors from that. I have also tried adding tags but was not able to successfully do that. I am using Swift 4.
Here is the code of my view controller:
import UIKit
class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ foodTypeUIPickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var countrows : Int = nutritionPickerData.count
if pickerView == foodTypeUIPickerView { **[Error Here: "Binary operator '==' cannot be applied to operands of type '_' and 'UIPickerView'"]**
countrows = self.foodTypePickerData.count
}
return countrows
}
func pickerView(_ foodTypeUIPickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == nutritionUIPickerView { **[Error Here: "Binary operator '==' cannot be applied to operands of type '_' and 'UIPickerView'"]**
let titleRow = nutritionPickerData[row]
return titleRow
} else if pickerView == foodTypeUIPickerView { **[Error Here: "Binary operator '==' cannot be applied to operands of type '_' and 'UIPickerView'"]**
let titleRow = foodTypePickerData[row]
return titleRow
}
return ""
}
#IBOutlet weak var nutritionUIPickerView: UIPickerView!
#IBOutlet weak var foodTypeUIPickerView: UIPickerView!
var nutritionPickerData = ["Protein", "Fiber", "Iron", "Fat", "Sodium", "Calcium/Vitamin D", "Energy", "Carbohydrates", "Cholestorol"]
var foodTypePickerData = ["Fruits", "Vegetables", "Legumes and Beans", "Grain", "Meat", "Dairy"]
#IBAction func submitUIButton(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.nutritionUIPickerView.delegate = self
self.nutritionUIPickerView.dataSource = self
self.foodTypeUIPickerView.delegate = self
self.foodTypeUIPickerView.dataSource = self
}
}
I need to use 2 picker views in 1 view controller. How do I do this?
You are referencing a non-existent variable named pickerView. You should update the parameter name to pickerView to avoid issues.
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == foodTypeUIPickerView {
return self.foodTypePickerData.count
} else {
return self. nutritionPickerData.count
}
}
Make a similar change to the other delegate/data source methods.
You should separate your data sources and delegates and not put them inside your view controller. Set those as the delegate and datasource. Code written in swift 4.2 xcode 10.1
class ViewController: UIViewController {
#IBOutlet weak var foodPicker: UIPickerView!
#IBOutlet weak var nutritionPicker: UIPickerView!
var foodData = FoodData()
var nutritionData = NutritionData()
override func viewDidLoad() {
super.viewDidLoad()
foodPicker.delegate = foodData
foodPicker.dataSource = foodData
nutritionPicker.delegate = nutritionData
nutritionPicker.dataSource = nutritionData
}
#IBAction func actionButton(_ sender: UIButton) {
let food = foodPicker.delegate?.pickerView!(foodPicker, titleForRow: foodPicker.selectedRow(inComponent: 0), forComponent: 0)
let nutrition = nutritionPicker.delegate?.pickerView!(nutritionPicker, titleForRow: nutritionPicker.selectedRow(inComponent: 0), forComponent: 0)
print("You picked: \(food) and \(nutrition)")
}
}
class FoodData: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
var foodTypePickerData = ["Fruits", "Vegetables", "Legumes and Beans", "Grain", "Meat", "Dairy"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return foodTypePickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return foodTypePickerData[row]
}
}
class NutritionData: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
var nutritionPickerData = ["Protein", "Fiber", "Iron", "Fat", "Sodium", "Calcium/Vitamin D", "Energy", "Carbohydrates", "Cholestorol"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return nutritionPickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return nutritionPickerData[row]
}
}
I can't figure out why I can't access this variable, see below, I want the value of sport result when it's clicked, however sportResult.selectedSport returns nil?
let sportPickerData = SportPickerData()
override func viewDidLoad() {
super.viewDidLoad()
sportPickerView.delegate = sportPickerData
sportPickerView.dataSource = sportPickerData
class SportPickerData: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
var sports = ["Football", "Baseball", "Hockey", "Basketball"]
var selectedSport: String?
func numberOfComponentsInPickerView(yearPicker: UIPickerView) -> Int {
return 1
}
func pickerView(yearPicker: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return sports.count
}
func pickerView(yearPicker: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedSport = sports[row]
}
func pickerView(yearPicker: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return "\(sports[row])"
}
}
let sportResult = SportPickerData()
#IBAction func button(sender: UIButton) {
label.text = sportResult.selectedSport
}
sportResult its not a delegate or a data source of your picker view so its selectedSport property will never be initialized since its didSelectRow method will be never called. To get the selectedSport you have to get it from sportPickerData which is actually the delegate and data source of your picker
let sportResult = SportPickerData() // delete this line
#IBAction func button(sender: UIButton) {
label.text = sportPickerData.selectedSport
}
I am trying to add a pickerview to my iphone application but instead of showing string from my array it shows question marks. Does anyone know why? I've been trying to figure it out for the past hour... Here is my code of the controller that contains the pickerview:
class NewIssueViewController: UIViewController, UIPickerViewDelegate {
var componentArr = ["component1","component2","component3","component4"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func CancelPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func AddPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
return componentArr.count
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return componentArr[row]
}
}
You set your picker view's datasource, but didn't set its delegate.
As a result pickerView:titleForRow: never gets called.
You should either connect the PickerView's Datasource and Delegate in the storyboard or set them programatically. See the image below.
New Swift 3 has deprecated usage of above methods,
i have implemented the following to make them work. (Some underscore added in param.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerDataSource.count;
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerDataSource[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
/*if(row == 0)
{
self.view.backgroundColor = UIColor.whiteColor();
}
else if(row == 1)
{
self.view.backgroundColor = UIColor.redColor();
}
else if(row == 2)
{
self.view.backgroundColor = UIColor.greenColor();
}
else
{
self.view.backgroundColor = UIColor.blueColor();
}*/
}
Just add self.pickerOutletReference.delegate = self on viewDidLoad() menthod