Acces to viewController from a pickerView - ios

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.

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]
}
}

2 TextField with 2 picker in 1 UIView

I made a 2 textField with 2 pickers in 1UIView but I can't show the right pickers in the right textfield because I don't know how recognize the current view active.
I tried something with tag (but only work with pickers, no with textfield), and create var current = UITextField()
var genre = ["-","Homme","Femme"]
var Sport = ["-","Tennis"]
var picker = UIPickerView()
var currentTextField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
GenderTextField.inputView = picker
WitchSportTextField.inputView = picker
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if currentTextField == GenderTextField{
return genre.count
}else{
return Sport.count
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if currentTextField == GenderTextField {
GenderTextField.text = genre[row]
}else{
WitchSportTextField.text = Sport[row]
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if currentTextField == GenderTextField{
return genre[row]
}else{
return Sport[row]
}
}
Someone as an idea ?
here is the complete code
var genre = ["-","Homme","Femme"]
var Sport = ["-","Tennis"]
var genrepicker = UIPickerView()
var Sportpicker = UIPickerView()
var genretTextField = UITextField()
var SportTextField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
genrepicker.delegate = self
genrepicker.dataSource = self
Sportpicker.delegate = self
Sportpicker.dataSource = self
genretTextField.inputView = genrepicker
SportTextField.inputView = Sportpicker
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == genrepicker{
return genre.count
}else{
return Sport.count
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == genrepicker {
GenderTextField.text = genre[row]
}else{
WitchSportTextField.text = Sport[row]
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == genrepicker{
return genre[row]
}else{
return Sport[row]
}
}
Firstly assign your both TextField delegate to self in viewDidLoad
GenderTextField.delegate = self
WitchSportTextField.delegate = self
Use this extension for identified which TextField is active.
extension YourClass: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
currentTextField = textField
picker.reloadAllComponents() //Reload for accurate data
}
}
NOTE: Variable names should begin with lower case letter. Use descriptive names to clarify your intent:
Look at this: https://github.com/MLSDev/development-standards/blob/master/platform/ios/swift-coding-conventions.md

How to Show only one Component when UIPickerView have Multiple Components?

I have two Componenets in UIPickerView. One is for country and another is for city. I want to show only one component when clicked on Country Button and another one will show when clicked on the City Button.
#IBOutlet weak var myPicker: UIPickerView!
var pickerData = [["India", "Pakistan", "Bangladesh", "USA", "Afganistan", "Russia", "Nepal", "Bhutan"], ["Chandigarh", "Punjab", "Ludhiana", "Amritsar", "Shimal", "Una"]]
override func viewDidLoad() {
super.viewDidLoad()
myPicker.isHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData[component].count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[component][row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// print(pickerData[row])
showCountry.text = pickerData[component][row]
}
#IBAction func showCountry(_ sender: Any) {
myPicker.isHidden = false
}
For Now when I click on country, it shows both components but I just want to show one componenet in pickerview.
you can do it by just handling it in conditions as requirement
import UIKit
class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource {
var pickerData = [["India", "Pakistan", "Bangladesh", "USA", "Afganistan", "Russia", "Nepal", "Bhutan"], ["Chandigarh", "Punjab", "Ludhiana", "Amritsar", "Shimal", "Una"]]
#IBOutlet weak var myPicker: UIPickerView!
var check : Int = 0
override func viewDidLoad() {
super.viewDidLoad()
myPicker.isHidden = true
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
if check == 1 {
return 1
}
else
{
return 1
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData[component].count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if check == 1 {
return pickerData[0][row]
}
else
{
return pickerData[1][row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// print(pickerData[row])
// showCountry.text = pickerData[component][row]
}
#IBAction func showCountry(_ sender: Any) {
check = 1
myPicker.reloadAllComponents()
myPicker.isHidden = false
}
#IBAction func showcity(_ sender: Any) {
check = 2
myPicker.reloadAllComponents()
myPicker.isHidden = false
}
}
Hope this will help you
If you want select one picker and country and city
numberOfComponents
should be 2
or if you want select one picker and country than select again to city
numberOfComponent
should be 1
but careful this state
//e.g
#IBOutlet weak var myPicker: UIPickerView!
var pickerData = [["India", "Pakistan", "Bangladesh", "USA", "Afganistan", "Russia", "Nepal", "Bhutan"], ["Chandigarh", "Punjab", "Ludhiana", "Amritsar", "Shimal", "Una"]]
var isSelectingCountry = true
var selectedCountry: String!
var selectedCity: String!
override func viewDidLoad() {
super.viewDidLoad()
myPicker.isHidden = true
}
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[isSelectingCountry ? 0 : 1].count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let index = isSelectingCountry ? 0 : 1
return pickerData[index][row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// print(pickerData[row])
//showCountry.text = pickerData[component][row]
if isSelectingCountry {
isSelectingCountry = false
selectedCountry = pickerData[0][row]
} else {
selectedCity = pickerData[1][row]
}
}
#IBAction func showCountry(_ sender: Any) {
// if you want reset picker for start with country uncomment below line
//isSelectingCountry = true
myPicker.isHidden = false
}
and you can use selectedCity and selectedCountry

Multiple pickerviews in single viewController

Is it possible to have a single pickerView in swift which changes its value based on the button pressed. I could not figure out a way to implement more than one parameter through pickerView in a single ViewController. An alternative solution like a dropdown menu is fine too. Example/functional code would be great if provided. This is for a quiz app which would take multiple parameters.
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var pickerView: UIPickerView!
#IBOutlet weak var selectionBtn: UIButton!
let animals = ["Lion", "Dog", "Monkey", "Cat", "Bat"]
override func viewDidLoad() {
pickerView.isHidden = true
pickerView.delegate = self
pickerView.dataSource = self
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func selectPressed(_ sender: UIButton) {
if pickerView.isHidden {
pickerView.isHidden = false
}
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return animals.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return animals[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectionBtn.setTitle(animals[row], for: .normal)
pickerView.isHidden = true
}
}
Yes by having more than 1 data source
let animals1 = ["Lion", "Dog", "Monkey", "Cat", "Bat"]
let animals2 = ["Lion", "Dog", "Monkey", "Cat", "Bat"]
var mainData = [String]()
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return mainData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, fo rComponent component: Int) -> String? {
return mainData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectionBtn.setTitle(mainData[row], for: .normal)
pickerView.isHidden = true
}
assign mainData to say animal1
and reload
pickerView.reloadAllComponents()
Keep a separate array and put your array into it according to your logic. Use that array in pickerview. Try This:
let animals = ["Lion", "Dog", "Monkey", "Cat", "Bat"]
let secondAnimals = ["Lion1", "Dog1", "Monkey1", "Cat1", "Bat1"]
let pickerArray = [String]()
#IBAction func selectPressed(_ sender: UIButton) {
pickerArray = animals //change it according to your logic
pickerView.reloadAllComponents()
pickerView.isHidden = !pickerView.isHidden
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerArray.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerArray[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectionBtn.setTitle(pickerArray[row], for: .normal)
pickerView.isHidden = true
}
You can have two arrays to populate the picker like this
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if numb == 1
{
return firstArray.count
}
else
return secondArray.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if numb == 1
{
return firstArr[row]
}
else
{
return secondArr[row]
}
}
Whenever you have to change the data source for datepicker, make sure after that you are calling method
picker.reloadAllComponents()
Take flag and use appropriate data source.
let say you added 2 PickerView to your View
- set the 1st picker´s tag as 1 & 2 for the 2nd picker under the Attributes Inspector
- CTRL + drag from each picker to the top yellow View Controller icon and choose dataSource.Repeate the same choosing delegate
- repeate the above for the other picker too
- add pickerview & pickerviewdelegation to your ViewController class:
class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
in your ViewController class, create empty arrays for the pickers:
var picker1Options = []
var picker2Options = []
on viewDidLoad() method, populate the arrays with your content:
picker1Options = ["p1 1","p1 2","p1 3","p1 4","p1 5"]
picker2Options = ["p2 1","p2 2","p2 3","p2 4","p2 5"]
implement the delegate & pickerview methods:
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 1){
return picker1Options.count
}else{
return picker2Options.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if (pickerView.tag == 1){
return "\(picker1Options[row])"
}else{
return "\(picker2Options[row])"
}
}
Hope this will help.
let arrAny : [String] = [String]()
button action:
#IBAction func btn2Pressed(_ sender: UIButton) {
arrAny = ["Lion1", "Dog1", "Monkey1", "Cat1", "Bat1"]
if pickerView.isHidden {
pickerView.isHidden = false
}
}
#IBAction func btn1Pressed(_ sender: UIButton) {
arrAny = ["Lion", "Dog", "Monkey", "Cat", "Bat"]
if pickerView.isHidden {
pickerView.isHidden = false
}
}
pickerView delegate:
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return arrAny.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return arrAny[row]
}

Having multiple Picker Views

I am trying to have multiple picker views in one single view in swift.
So far I have created 2 different textfields and I want both of them to have different picker views if I click on them. so let us say if click on textfield #1 it opens the array #1 and textfields #2 the second.
I have already looked up these questions but they do not really answer my question or solve my problem:
Multiple Picker Views on a single view
Creating Multiple Dynamic Picker Views
so my View Controller looks like this:
how my viewController should look
and this is my piece of code:
let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()
//picker View functions
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickOption.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickOption[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
weightTextField.text = pickOption[row]
}
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
weightTextField.inputView = pickerView
weightTextField.text = pickOption[0]
}
so as mentioned before, the first textfield opens up the array 'pickOption' and the second array opens up 'ageOption'
EDIT: 'weightTextField' ist the first textfield which opens up the first array(pickOption)
We can do it for multiple UITextField from single PickerView, with the help of UITextFieldDelegate
let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()
var currentTxtFldTag : Int = 10
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
weightTextField.tag = 10
ageTextField.tag = 20
weightTextField.delegate = self
ageTextField.delegate = self
weightTextField.inputView = pickerView
weightTextField.text = pickOption[0]
ageTextField.inputView = pickerView
ageTextField.text = ageOption[0]
// Do any additional setup after loading the view.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if currentTxtFldTag == 10
{
return pickOption.count
}
else
{
return ageOption.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if currentTxtFldTag == 10
{
return pickOption[row]
}
else
{
return ageOption[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if currentTxtFldTag == 10
{
weightTextField.text = pickOption[row]
}
else
{
ageTextField.text = ageOption[row]
}
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField.tag == 10 // WEIGHT OPTION
{
currentTxtFldTag = 10
}
else // AGE OPTION
{
currentTxtFldTag = 20
}
pickerView.reloadAllComponents()
return true
}
Output
Suppose that you have two pickerView controls: picker0 and picker1.
class HomeViewController: BasicViewController, UITextFieldDelegate, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
let numberArray = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180]
var areaArray = [String]()
#IBOutlet weak var picker0: UIPickerView!
#IBOutlet weak var picker1: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// area //
for i in 0..<11 {
let str = "Area "
let localStr = NSLocalizedString(str + String(i), comment: "")
areaArray.append(localStr)
}
}
// MARK: - Campaign duration & area
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == picker0 {
return numberArray.count
} else {
return areaArray.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == picker0 {
return String(numberArray[row]) + " pieces"
} else {
return areaArray[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == seventhPicker {
} else {
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
weightTextField.inputView = pickerView
}
let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()
//picker View functions
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if weightTextField1.tag == 1 {
return pickOption.count
} else if weightTextField2.tag == itemPicker {
return ageOption.count
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if weightTextField1.tag == 1 {
return pickOption[row]
} else if weightTextField2.tag == 1 {
return ageOption[row]
}
return 0
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if weightTextField1.tag == 1 {
weightTextField1.text = pickOption[row]
} else if weightTextField2.tag == 1 {
weightTextField2.text = ageOption[row]
}
}
////set delegate
func textFieldDidBeginEditing(textField: UITextField!) { //delegate method
if textField == weightTextField1
{
weightTextField1.atg = 1;
weightTextField2.atg = 0;
}
else if textField == weightTextField2
{
weightTextField1.atg = 0;
weightTextField2.atg = 1;
}
picker.reloadAllComponents()
}
}

Resources