Linking a UIPickerView row title to a new view controller - ios

I have a UIPickerView with some rows each having a title. What I want is when one clicks on a title, a new view controller specific to that title is linked to just like how clicking a button takes me to a new VC. How can I do that with a row title in a picker view?
I am trying to drag a specific row into the view controller but the whole picker get selected.

You can set pickerView delegate and after you can push viewController
Example code:
class ViewController: UIViewController {
#IBOutlet private weak var pickerView: UIPickerView!
struct PickerData {
let viewControllerId: String
let storyboardId: String
let title: String
}
private var pickersData: [PickerData] = []
override func viewDidLoad() {
super.viewDidLoad()
loadData()
pickerView.delegate = self
pickerView.dataSource = self
}
private func loadData() {
pickersData.append(
ViewController.PickerData(viewControllerId: "ViewController1", storyboardId: "Main", title: "Foo")
)
pickersData.append(
ViewController.PickerData(viewControllerId: "ViewController1", storyboardId: "Main", title: "Bar")
)
}
private func createViewController(from index: Int) -> UIViewController {
let model = pickersData[index]
let viewController = UIStoryboard(name: model.storyboardId, bundle: nil).instantiateViewController(withIdentifier: model.viewControllerId)
return viewController
}
private func pushViewController(viewController: UIViewController) {
// If Navigation
navigationController?.pushViewController(viewController, animated: true)
// If present
//present(viewController, animated: true, completion: nil)
}
}
extension ViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickersData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickersData[row].title
}
}
extension ViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pushViewController(viewController: createViewController(from: row))
}
}

#IBOutlet weak var myPickerView: UIPickerView!
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
performSegue(withIdentifier: "segue\(row)", sender: nil)
}
drag the segues from your view, not the pickerView

Related

Not able to save selected value from a UIPickerView to an array

So far I have tried to use
func pickerView1(_ helpTypePicker: UIPickerView, didSelectRow row: Int, inComponent component: Int {
infoListInit.requestInfoList[4] = (helpTypePickerValues[row])
}
But the value is just not saving to the array. I tried printing the value after saving it, but to no avail. I do have helpTypePicker as a self delegate in my viewDidLoad func, so I don't know where to go from here. Other text values from UITextFields save fine, but none of my UIPickerViews are working in this regard
Here is the full class:
import Foundation
import UIKit
class requestHelpTypePickerScreen: UIViewController,
UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent
component: Int) -> Int {
if pickerView.tag == 1 {
return helpTypePickerValues.count
} else {
return languagePickerValues.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return "\(helpTypePickerValues[row])"
} else {
return "\(languagePickerValues[row])"
}
}
// Mark: outlets
#IBOutlet weak var helpTypePicker: UIPickerView!
#IBOutlet weak var languagePicker: UIPickerView!
#IBOutlet weak var helpTypePickerLabel: UILabel!
let helpTypePickerValues = ["Academic/Professional", "Donate", "Food/Groceries", "Medication", "Emotional Support", "Misc."]
let languagePickerValues = ["Arabic", "Chinese", "Spanish", "English", "French", "Hindi/Urdu", "Korean", "Russian"]
override func viewDidLoad() {
super.viewDidLoad()
helpTypePicker.delegate = self
languagePicker.delegate = self
self.helpTypePicker.selectRow(2, inComponent: 0, animated: true)
self.languagePicker.selectRow(3, inComponent: 0, animated: true)
}
// Mark: actions
#IBAction func buttonPressed(_ sender: Any) {
func pickerView(_ helpTypePicker: UIPickerView,
didSelectRow row: Int,
inComponent component: Int) {
infoListInit.requestInfoList[5] = (helpTypePickerValues[row])
}
// this is called pickerView1 because it would conflict with the above
// function if called pickerView
func pickerView1(_ languagePicker: UIPickerView,
didSelectRow row1: Int,
inComponent component: Int) {
infoListInit.requestInfoList[6] = (languagePickerValues[row1])
}
performSegue(withIdentifier: "pickersDone", sender: nil)
}
There is a mistake in your code, the Delegate indicates that when a new row is selected in the UIPickerView, the function pickerView(_:didSelectRow:inComponent:) will be called (the argument name doesn't matter).
In your case in when any of the pickers select a new row pickerView(_:didSelectRow:inComponent:) is called with the picker passed as arguments in the firs position. You should check which picker was modified and then change your logic.
In your case:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == helpTypePicker {
infoListInit.requestInfoList[5] = helpTypePickerValues[row]
} else {
infoListInit.requestInfoList[6] = languagePickerValues[row]
}
}

How can I read and set a variable in another class?

In my searches, I see this is a common question but I've tried some of the solutions and haven't been able to resolve. A simple example below. When I run the app, and press button, 0 prints as expected. After pressing button2, then button, 1 prints, also as expected. If I move the picker, then press button, I expect 2, but still get 1.
I gather this is because ViewController().sample() is creating a copy, and not actually changing the variable in the "original" ViewController(). I'm stumped at what needs to be reconfigured to get the desired behavior.
import UIKit
class pickerHelper: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
let data: [String]
init(data: [String]) {
self.data = data
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
data.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
data[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
ViewController().sample()
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
p1.delegate = helper1
p1.dataSource = helper1
}
#IBOutlet weak var p1: UIPickerView!
let helper1 = pickerHelper(data:["a", "b", "c"])
var test = Int()
#IBAction func button(_ sender: Any) {
print(test)
}
#IBAction func button2(_ sender: Any) {
test = 1
}
func sample() {
test = 2
}
}
This is a situation where you would either use a delegation pattern or a callback closure; this latter is more "Swifty".
With delegation:
protocol PickerHelperDelegate {
func pickerHelper(_ helper: selectedRow row:)
}
Then in your PickerHelper you add a delegate property and invoke the delegate when required:
class PickerHelper: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
let data: [String]
var delegate: PickerHelperDelegate?
init(data: [String]) {
self.data = data
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
data.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
data[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.delegate?.pickerHelper(self, selectedRow: row)
}
}
In your view controller you need to assign the delegate and implement the protocol function:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
p1.delegate = helper1
p1.dataSource = helper1
helper1.delegate = self // Assign this object as the picker helper's delegate
}
#IBOutlet weak var p1: UIPickerView!
let helper1 = pickerHelper(data:["a", "b", "c"])
var test = Int()
}
extension ViewController: PickerHelperDelegate {
func pickerHelper(_ helper: PickerHelper, selectedRow row:Int) {
self.sample()
print("Selected row \(row)")
}
}
You can do a similar thing with a closure:
class PickerHelper: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
let data: [String]
var changeHandler: ((Int)->Void)?
init(data: [String]) {
self.data = data
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
data.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
data[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.changeHandler?(row)
}
}
In your view controller you need to assign a closure to the changeHandler property:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
p1.delegate = helper1
p1.dataSource = helper1
helper.changeHandler = { row in
self.sample()
print("Selected row \(row)")
}
}
You could also change your protocol or closure to pass back the data element rather than just the row number and use generics for your PickerHelper so that it wasn't just limited to strings.
Note that classes start with a capital by convention, so you should say PickerHelper, not pickerHelper.
You can use the static property to keep its state in class.
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
p1.delegate = helper1
p1.dataSource = helper1
}
#IBOutlet weak var p1: UIPickerView!
let helper1 = pickerHelper(data:["a", "b", "c"])
static var test = Int()
#IBAction func button(_ sender: Any) {
print(test)
}
#IBAction func button2(_ sender: Any) {
ViewController.test = 1
}
static func sample() {
test = 2
}
}

How to get data selected from UIPickerView and display it on a different View Controller?

I would like to show a user different screens depending on what they choose in a UIPickerView. What would be the best way to implement this? Thanks!
import UIKit
class SelectKitViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var kitPickerView: UIPickerView!
let kits = ["Kit 1", "Kit 2"]
var chosenKit: String?
override func viewDidLoad() {
super.viewDidLoad()
kitPickerView.delegate = self
kitPickerView.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return kits[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return kits.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let value = kits[row]
chosenKit = value
}
#IBAction func continuePressed(_ sender: Any) {
performSegue(withIdentifier: "SelectReactionVolume", sender: nil)
}
}
I would like to perform a segue to another View Controller when continue is pressed. Once the user chooses a kit they will have to enter additional info about the kit selected.

Swift 4.2 - PickerView not showing Content

this code makes me really mad. I already read thousands of tutorials and stack overflow comments....
Please help me. The Picker just contains a blank grey screen.
Code here:
GenderPickerModel.swift
import UIKit
class GenderPickerModel: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
let genderPickerData: [String] = ["male", "female"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1;
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return genderPickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return genderPickerData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
}
and used in a Controller:
#IBOutlet weak var gender: UITextField!
private var genderPickerView: UIPickerView?
override func viewDidLoad() {
super.viewDidLoad()
loadProfile()
prepareView()
}
func prepareView() {
genderPickerView = UIPickerView.init()
let gpModel = GenderPickerModel()
genderPickerView?.delegate = gpModel
genderPickerView?.dataSource = gpModel
gender.inputView = genderPickerView;
genderPickerView?.selectRow(0, inComponent: 0, animated: true)
genderPickerView?.reloadAllComponents()
}
First, you do not need to do the separate array of gender in pickerview class and you don't need to create the instance of pickerview.
From this, you can see your datasource in your custompickerview
/**
Controller code where you can create instantiate picker assign data
*/
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
let genderPickerData : [String] = ["male", "female"]
override func viewDidLoad() {
super.viewDidLoad()
prepareView()
}
func prepareView() {
genderPickerView = UIPickerView()
let gpModel = GenderPickerModel()
gpModel.data = genderPickerData
gpModel.dataSource = gpModel
gpModel.delegate = gpModel
textField.inputView = gpModel
}
}
/**
Custom pickerview class where you can handle data
*/
import UIKit
class GenderPickerModel: UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate {
var data = [String]()
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return data.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return data[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
}

How to navigate between two viewcontrollers using a UIPickerview

I am trying to navigate between two viewcontrollers using a UIPickerview, so i first learned how to use didSelectRow to change the title of a label and then i tried using some code to navigate to another viewcontroller but that didn't work.
The code that i used was:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet var label: UILabel!
#IBOutlet var pickerView: UIPickerView!
var food = ["hello", "hi", "hey"]
var placementAnswer = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pickerView.delegate = self
pickerView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
public func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return food.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return food[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
placementAnswer = row
}
#IBAction func labelChanged(sender: UIButton) {
if (placementAnswer == 0){
}else if (placementAnswer == 1){
let view2 = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as! ViewController2
self.navigationController?.pushViewController(view2, animated: true)
}else if (placementAnswer == 2){
}
}
}
As paranoidcoder pointed out I'm not sure your labelChanged method is getting called, which could be the problem with your existing code.
I'm assuming you want to change the view controller after a certain row is selected in the picker view. Putting the if statement to change views in didSelectRow would make the most sense in that case.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
placementAnswer = row // unnecessary variable
if (placementAnswer == 0){}
else if (placementAnswer == 1)
{
let view2 = self.storyboard?.instantiateViewControllerWithIdentifier("view2") as! ViewController2
self.navigationController?.pushViewController(view2, animated: true)
}
else if (placementAnswer == 2) {}
}

Resources