I'm trying to do a pickerView and a button which when you choose your option in pickerView it goes to the relevant page.
So far I've got to the point when you choose the option in picker view it goes to the relevant page, but what I want is choose and then click on the button to go to the page.
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var pickerView: UIPickerView!
var selection = ["Page 1", "Page 2", "Page 3"]
override func viewDidLoad() {
super.viewDidLoad()
pickerView.dataSource = self
pickerView.delegate = self
}
#IBAction func SelectionBtn(_ sender: Any) {
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return selection.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let selected = selection[row]
return selected
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (row == 0) {
performSegue(withIdentifier: "red", sender: self)
}
else if (row == 1) {
performSegue(withIdentifier: "blue", sender: self)
}
else {
performSegue(withIdentifier: "green", sender: self)
}
}
}
Take an extra variable to store information on regarding which row you have selected.
var rowSelected:Int! = -1
Store the selected row to this variable.
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
rowSelected = row
}
During button action check the variable to perform desired Segue.
#IBAction func actSegue(sender:Any){
if row == 0 {
performSegue(withIdentifier: "red", sender: self)
}...
....
}
You can get the row selected with :pickerView.selectedRow(inComponent: 0)
#IBAction func SelectionBtn(_ sender: Any) {
let rowSelected = pickerView.selectedRow(inComponent: 0)
if (rowSelected == 0) {
performSegue(withIdentifier: "red", sender: self)
}
else if (rowSelected == 1) {
performSegue(withIdentifier: "blue", sender: self)
}
else {
performSegue(withIdentifier: "green", sender: self)
}
}
Related
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]
}
}
I'm trying to write a color picker for a task app. When I try to add the task the picker never makes the changes to my 'picked' variable (declared on line 5) and will only print "blank" (line 7). I'm new enough to pickers that I can't see what I'm doing wrong. I'm fairly certain it has to do with the setup of my didSelectRow function (last function), because if the function were firing, it would still set something in the else statement, even if it's not the string I want, but it's not making any changes at all.
class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var input: UITextField!
#IBOutlet weak var picker: UIPickerView!
var pickerData: [String] = [String]()
var picked: String = "blank"
#IBAction func addItem(_ sender: Any) {
print (picked)
if (input.text != "" && picked != "") {
list.append(input.text! as AnyObject)
if picked == "Red" {
colors.append(UIColor.red)
}
else if picked == "Orange" {
colors.append(UIColor.orange)
}
else if picked == "Yellow" {
colors.append(UIColor.yellow)
}
else if picked == "Green" {
colors.append(UIColor.green)
}
else if picked == "Blue" {
colors.append(UIColor.blue)
}
else if picked == "Purple" {
colors.append(UIColor.purple)
}
else if picked == "White" {
colors.append(UIColor.white)
}
input.text = ""
userDefaults.set(list, forKey: "tasks")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.picker.delegate = self
self.picker.dataSource = self
pickerData = ["Select a color", "White", "Red", "Orange", "Yellow", "Green", "Blue", "Purple"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if row == 0 {
picked = ""
}
else {
picked = pickerData[row]
}
}
}
You forget argument label _
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
On a sight I have a PickerView and a button. Depending on the choice in the pickerview (I have 4 lines) I would like clicking on a button to go on a specific view. Now, whatever my choice is, I'm always on the same view (saline). Below is my code of the controller. If anyone can help me understand my mistake?
import UIKit
class SommaireGeneralViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var picker: UIPickerView!
var PlacementAnswer = 0
var liste = ["Sommaire général", "Région Est", "Région Ouest", "Région Sud", "Région Nord"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.picker.delegate = self
self.picker.dataSource = self
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return liste[row]
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return liste.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = liste[row]
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 10.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
return myTitle
}
#IBAction func Voir(sender: AnyObject) {
if (PlacementAnswer == 0) {
self.performSegueWithIdentifier("Saline", sender: self)
} else if (PlacementAnswer == 1){
self.performSegueWithIdentifier("1", sender: self)
return
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
PlacementAnswer = row
}
func prepareForSegue(id: UIStoryboardSegue, sender: AnyObject?) {
if "Saline" == id.identifier {
}
else if "1" == id.identifier {
}
}
}
Hello, finally it's working, I put the code that can be used for others.
Cheers and thank you again for the info
import UIKit
class SommaireGeneralViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var picker: UIPickerView!
enum ActionSheetTag: Int {
case ActionSheet0
case ActionSheet1
}
var PlacementAnswer = Int()
let liste = ["Sommaire général", "Région Est", "Région Ouest", "Région Sud", "Région Nord"]
override func viewDidLoad() {
super.viewDidLoad()
self.picker.delegate = self
self.picker.dataSource = self
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return liste[row]
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return liste.count
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let titleData = liste[row]
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 10.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
return myTitle
}
#IBAction func Voir(sender: AnyObject) {
if (PlacementAnswer == 0) {
self.performSegueWithIdentifier("Saline", sender: self)
} else if (PlacementAnswer == 1){
self.performSegueWithIdentifier("Autre", sender: self)
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if let tag = ActionSheetTag(rawValue: row) {
switch tag {
case .ActionSheet0:
PlacementAnswer = 0
case .ActionSheet1:
PlacementAnswer = 1
}
}
}
}
You tried tag and enum? If your answer is no, try this and return the output to me.
enum ActionSheetTag: Int {
case ActionSheet1
case ActionSheet2
}
.
.
.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if let tag = ActionSheetTag(rawValue: pickerView.tag) {
switch tag {
case .ActionSheet1:
PlacementAnswer = 0
case .ActionSheet2:
PlacementAnswer = 1
}
}
}
Hope it helps.
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){
}
}
}
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) {}
}