I have followed the following SO question to create multiple picker views.
It works but not exactly how I want it.
What I require:
My view will have multiple buttons. On tap each button will show a picker view at the bottom of the screen.Each buttons picker view will have different options. See image below:
What I have done:
So far I have created two buttons and then added two picker views to my storyboard. See Below
The code is below:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet var one: UIPickerView!
#IBOutlet var Two: UIPickerView!
var picker1Options = [String]()
var picker2Options = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
picker1Options = ["Option 1","Option 2","Option 3","Option 4","Option 5"]
picker2Options = ["Item 1","Item 2","Item 3","Item 4","Item 5"]
self.one.hidden = true
self.Two.hidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
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])"
}
}
#IBAction func oneShow(sender: AnyObject) {
self.one.hidden = false
}
#IBAction func twoShow(sender: AnyObject) {
self.Two.hidden = false
}
}
The output of the above code is:
The Issues:
As you can see in the above image that although I can show different picker views they are not properly displayed. I would like each picker view to be displayed at the end bottom of the screen.
Also please note that I am using self.picker.hidden = true to hide and unhide the picker views. Is this the correct way of hiding and unhiding picker views? Is there a better way of achieving this functionality?
On the storyboard should I place picker views on top of each other at the bottom of the screen so they always appear at bottom on triggering self.picker.hidden = false ?
Whats the proper way of implementing multiple picker views on storyboard?
Any help will be greatly appreciated.
You make some big concept mistake in the usage of the UiPickerView:
1.You need only one UIPickerView in your storyboard.
2.You need only one data source for the picker view (on button click you gonna change the values in it).
I make an example by your code but be aware there will be some mistakes because i write it on NotePad++ (i don't have Mac in front of me).
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet var picker: UIPickerView!
var pickerOptions1 = [String]()
var pickerOptions2 = [String]()
var pickerOptions3 = [String]()
var dataSource;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
pickerOptions1 = ["Option 1","Option 2","Option 3","Option 4","Option 5"]
pickerOptions2 = ["Item 1","Item 2","Item 3","Item 4","Item 5"]
pickerOptions3 = ["Something 1","Something 2","Something 3","Something 4","Something 5"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataSource.count;
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return "\(dataSiurce[row])"
}
#IBAction func showPickerOne(sender: AnyObject) {
dataSource = pickerOptions1;
[self.picker reloadAllComponents];
}
#IBAction func showPickerTwo(sender: AnyObject) {
dataSource = pickerOptions2;
[self.picker reloadAllComponents];
}
#IBAction func showPickerThree(sender: AnyObject) {
dataSource = pickerOptions3;
[self.picker reloadAllComponents];
}
}
I hope this will help you understand the concept of pickers in ios.
You can take textfield instead of buttons and add picker view as inputview of textfield. And based on the textfield you can change the contents of array and displayed in pickerview.
Please find image in that i used age picker and team picker.
EDIT:
-(void) viewWillAppear:(BOOL)animated
{
[self.PickerVw removeFromSuperview];
txtAge.inputView = self.PickerVw;
txtSportName.inputView = self.PickerVw;
}
#pragma mark - UItextfield delegate Methods
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
arr1 = [NSMutableArray new];
if (textField.tag == 1)
{
int min = 9;
int max = 100;
for (int i = min ; i < max; i++)
{
[arr1 addObject:[NSString stringWithFormat:#"%d",(i+1)]];
}
[textField becomeFirstResponder];
self.picker.tag = 1;
}
if (textField.tag == 2)
{
[self.picker selectRow:0 inComponent:0 animated:NO];
arr1 = [arrSports mutableCopy];
[textField becomeFirstResponder];
self.picker.tag = 2;
}
[self.picker reloadAllComponents];
}
Here's a sample code on how Apple shows/hides a datePicker which should point you to the right direction.
Related
I have 2 UITableViews in a single View Controller which represent a 23 cells hours table view and 60 cells minutes table view. What i want to achieve is i can scroll both table view like rolling wheel like when we have scrolled to the end, it can still be scrollable to the first table view by scrolling bottom and vice versa.
I found something like this : https://github.com/bharath2020/UITableViewTricks/tree/master/CircleView/BBTableView , but too bad it's in Obj-c.
I want to be able to achieve the following :
Where the hours and minutes can be scrolled infinitely like from 12 -> 0 or 59 -> 0
How do i do something like that in swift?
Okay So just tested out for you. Here is an example for the circular UIPickerView.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let maxRows = 10
private let maxElements = 10000
#IBOutlet weak var myPickerView: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
self.myPickerView.dataSource = self;
self.myPickerView.delegate = self;
myPickerView.selectRow(maxElements / 2, inComponent: 0, animated: false)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return maxElements
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let myRow = row % maxRows
let myString = String(format: "%i", myRow)
return myString
}
}
Now as far as creating a UIPickerPickerView with two components is concerned, You can find easily on google.
I hope it helps you to solve the problem.
I'm making a GPA calculator using swift and I've run into some problems. I created a picker view with all the letter grades (A to F). I've put 7 text fields (one for each course). I want the user to tap on a textfield and the pickerview should appear, and the grade selected would appear in the textfield. I've managed to do that for the first textfield but I dont know how to write the code that allows me to pick the grade for each textfield. I've tried different methods but I always end up changing the first textfield only.
Any help is appreciated :-)
Here is my code (gradeOne is a textfield for course 1, gradeTwo for course 2, etc.):
import UIKit
class GPA: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet var gradeOne: UITextField!
#IBOutlet var gradeTwo: UITextField!
//Grades array
var grades = ["A+/A (85-100)", "A- (80-84)", "B+ (77-79)", "B (73-76)", "B- (70-72)", "C+ (67-69)", "C (63-66)", "C- (60-62)", "D+ (57-59)", "D (53-56)", "D- (50-52)", "F (0-49)"]
//Grade 1 Picker
func numberOfComponentsInPickerView(gradePickerView: UIPickerView) -> Int {
return 1
}
func pickerView(gradePickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return grades.count
}
func pickerView(gradePickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return grades[row]
}
func pickerView(gradePickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
gradeOne.text = grades[row]
}
//Make keyboard disappear
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let gradePickerView = UIPickerView()
gradePickerView.delegate = self
gradeOne.inputView = gradePickerView
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
}
You should set gradePickerView as the inputView for all your textfields
gradeTwo.inputView = gradePickerView
gradeThree.inputView = gradePickerView
// set for rest of textfields
and then in your pickerView:didSelectRow: method you need to set the text to the active text field at the time. I suggest setting the active textfield using UITextFieldDelegate
var activeField: UITextField?
override func viewDidLoad() {
// Set VC as textfield's delegate
gradeTwo.delegate = self
gradeThree.delegate = self
// Set for rest of textfields
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
activeField = textField
}
func pickerView(gradePickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
activeField.text = grades[row]
}
Use the delegate for UITextField to set all inputviews to pickerView. Make sure you set delegate for all textfields
let gradePickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
// set delegates for all textfields
gradePickerView.delegate = self
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
view.addGestureRecognizer(tap)
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
textField.inputView = gradePickerView
}
I think you want to tap on 7 textfiel. Each time the pickerView will show. You picker a cell in pickerView and the textfiel(that you just tap) will show your select? If it's true. You should do fowllow this step:
1: Add tag to each textFiel: yourtextFiel.view.tag = 0 (or 1,2,3,4,...)
2: When show pickerView: Add pickerView.tag = yourtextFiel.view.tag
3: in fucion pickerView didSelectRow : get your textFiel's tag will show your selected: let mytag = pickerView.tag.
Find your textFiel with "mytag" and change your text
Googluck
I have a fully functional UIPickerView inside a UITableViewCell and would like to accomplish two things.
The UIPickerView displays 4 components, but the first will be seldom referred to. As a result, I would like to either A (preferred) have it display a blank for 0 and scroll to 1, or B modify the color so the 0 is not so dominant.
Secondly, I would like to hide the UIPickerView until selected.
I found both options, but all reside in a UIViewController and do not work within a UITableViewCell.
import UIKit
class DiveDurationTableViewCell: UITableViewCell, UIPickerViewDelegate, UIPickerViewDataSource
{
#IBOutlet var textfieldBizCat: UITextField!
#IBOutlet var pickerBizCat: UIPickerView! = UIPickerView()
// ---------------------------------------------------------------------------------------------
// MARK: - Internal Constant
let numberOfComponents: Int = 4
let FirstDigitComponentRows: Int = 2
let SecondDigitComponentRows: Int = 10
let ThirdDigitComponentRows: Int = 10
let DisplayMinuteComponentRows: Int = 1
let Minutes: String = "Min"
let MinNumber = 0
let MaxNumber = 9
private var diveTime: Array<Int> = [Int]()
var FirstDigit: Int = 0
var SecondDigit: Int = 0
var ThirdDigit: Int = 0
var DisplayMinutes: String = "Min"
// ---------------------------------------------------------------------------------------------
// MARK: - IBOutlets
#IBOutlet var diveTimePicker: UIPickerView!
// ---------------------------------------------------------------------------------------------
// MARK: - UITableVieCell Methods
//
// Called when we are initialized from a storyboard or nib file.
//
override func awakeFromNib()
{
super.awakeFromNib()
//
// We create an array of integers from 0 to 9.
//
for i in self.MinNumber ..< self.MaxNumber+1
{
self.diveTime.append(i)
}
self.diveTimePicker.selectRow(0, inComponent: 0, animated: true)
self.diveTimePicker.selectRow(0, inComponent: 1, animated: true)
self.diveTimePicker.selectRow(0, inComponent: 2, animated: true)
}
//
// The item has been selected in the table.
//
override func setSelected(selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
}
// ---------------------------------------------------------------------------------------------
// MARK: - UIPickerViewDataSource Methods
//
// Number of components in the picker.
//
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return self.numberOfComponents
}
//
// Number of rows in each component.
//
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
if component == 0
{
return self.FirstDigitComponentRows
} else if component == 1 {
return self.SecondDigitComponentRows
} else if component == 2 {
return self.ThirdDigitComponentRows
} else {
return self.DisplayMinuteComponentRows
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
if component == 0
{
return String(self.diveTime[row])
}
if component == 1
{
return String(self.diveTime[row])
}
if component == 2
{
return String(self.diveTime[row])
}
else
{
return self.DisplayMinutes
}
}
//
// The user has selected in item in the picker view.
//
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
}
}
UPDATE:
I am not asking how to create a picker view, as the above code works great. I am only asking how to customize it to make it more attractive.
UITableView is subclass of UIScrollview and UIPicker is scrollview too, you need handle a lot of thing to make both of them work together.
First thing is when you make scroll on UITableview cell with Picker, which one is first response? If Picker is first response and the size of picker is too big such as big as device screen, you will not able to scroll table view anymore. If tableview is first response, you can't scroll Picker because table view will scroll.
I suggest you add Picker View as subview of tableview/controller.view at position/frame of cell that you want to add Picker before, try to fake like Picker inside tableview cell.
I have a ViewController with a UIPickerView as a single control myPickerView which is of a class MyPickerView which I created as a sub-class of UIPickerView. I invoke myPickerView in ViewController viewDidLoad by myPickerView.viewDidLoad. However, this does not execute the source functions of MyPickerView.
I need a clarification of how I can make this work. My reason for MyPickerView is that it has a lot of special code that I did not want to clutter up the main ViewController. See the example code below:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myPickerView: MyPickerView!
override func viewDidLoad() {
super.viewDidLoad()
myPickerView.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import UIKit
var gSep = ","
class MyPickerView: UIPickerView , UIPickerViewDataSource, UIPickerViewDelegate {
var pickerData = [[" "],[" "]]
var category = [""]
var subCategory = [""]
var dictMenuList = [String:String]()
//MARK:- category/subcategory picker
func viewDidLoad() {
println("MyPickerView: viewDidLoad")
dictMenuList = ["Medical":"Sub-Cat 1.1,Sub-Cat 1.2,Sub-Cat 1.3,Sub-Cat 1.4,Sub-Cat 1.5,Sub-Cat 1.6,Sub-Cat 1.7",
"Taxes": "Sub-Cat 2.1,Sub-Cat 2.2,Sub-Cat 2.3,Sub-Cat 2.4",
"Bills": "Sub-Cat 3.1,Sub-Cat 3.2,Sub-Cat 3.3,Sub-Cat 3.4,Sub-Cat 3.5,Sub-Cat 3.6,Sub-Cat 3.7"]
println("MyPickerView dictMenuList: \(dictMenuList)")
self.reloadAllComponents()
let firstKey = self.loadPickerWithCategory(0)
self.loadPickerWithSubCategory(firstKey)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
println("MyPickerView: numberOfComponentsInPickerView \(pickerData.count)")
return pickerData.count
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData[component].count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if component == 0 {
let selectedKey = category[row]
loadPickerWithSubCategory(selectedKey)
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[component][row]
}
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
var pickerLabel = UILabel()
pickerLabel.textColor = UIColor.blackColor()
pickerLabel.text = pickerData[component][row]
pickerLabel.font = UIFont(name: pickerLabel.font.fontName, size: 17)
pickerLabel.textAlignment = NSTextAlignment.Center
return pickerLabel
}
func loadPickerWithCategory (row: Int) -> String{
println("loadPickerWithCategory")
category = [String](dictMenuList.keys)
println("MyPickerView: category: \(category)")
if category.isEmpty {
return ""
}
let n1 = dictMenuList.count
pickerData[0].removeAll(keepCapacity: true)
for i in 0 ..< n1
{
pickerData[0].append(category[i])
}
return category[row]
}
func loadPickerWithSubCategory (key: String) {
println("MyPickerView: loadPickerWithSubCategory")
let x = dictMenuList[key]
subCategory = x!.componentsSeparatedByString(gSep)
let n1 = subCategory.count
pickerData[1].removeAll(keepCapacity: true)
if subCategory.isEmpty {
return
}
for i in 0 ..< n1
{
pickerData[1].append(subCategory[i])
}
self.reloadAllComponents()
}
}
The method viewDidLoad is a view controller method, not a view method. A UIPickerView is a subclass of UIView, not UIViewController, so the system will not call your viewDidLoad method.
You need to override one or more of the init methods.
If you're loading your picker view from a Storyboard or XIB, you probably want to override initWithCoder.
If you're creating your picker in code, you probably want to override initWithFrame.
I sometimes create a method setup that I call from both initWithCoder: and from initWithFrame:. That way my setup code gets called regardless of how the view object is loaded.
I vaguely remember reading that there is a better way of handling this dueling initializers problem in Swift, but I don't remember what it is. (I'm still learning Swift.)
EDIT:
It just occurs to me that you can use the method awakeFromNib to do setup after your view has been loaded and all of it's outlets are set up. That's roughly equivalent to the viewDidLoad call for view controllers. I should have thought of that sooner.
(awakeFromNib is a method of NSObject, so it's a bit hard to find if you don't know it exists.)
First of all viewDidLoad() is a method of the UIViewController class and is called after the controller's view is loaded into the memory. Read more here. You can not use it in views.
So you should implement an init method inside your custom picker class. I'd recommend to override initWithFrame and initWithCoder and set up your component there.
And you will initialize your custom picker like this:
myPickerView = MyPickerView(frame: yourFrame)
I hope the title makes some sense but what I am trying to do is to set a field value to the item selected variable from my dataPicker. I have been able to make this work when there is only one field to set but my project will have multiple fields on each view that will call data from the dataPicker based on what field it is. I hope that is clear. Maybe as you look at the code it will.
I have set up a test project to limit things to this issue only. So my variable to tell the view what array to populate in the dataPicker is either season or sport. the field that will receive the data from the season/sport array is enterSeason and enterSport. When the picker has returned a value from season, I want to combine it with enter to create the var enterSeason to set that == itemSelected. This language is very new to me so I am trying the only way I have used before to combine text and variables in one value. It is obviously not working. Help is appreciated.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UIGestureRecognizerDelegate {
#IBOutlet var enterSeason: UITextField!
#IBOutlet var enterSport: UITextField!
var dataPickerView = UIPickerView()
var season = ["2013", "2014", "2015"] //multi-season
//var season = ["2015"] //single-season
var sport = ["Baeball", "Football", "Basketball", "Hokey"]
var activeField = []
override func viewDidLoad() {
super.viewDidLoad()
enterSeason.inputView = dataPickerView
dataPickerView.delegate = self
dataPickerView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return activeField.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return activeField[row] as! String
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var itemSelected = activeField[row] as! String
self.enter"\activeField".text = itemSelected
}
}
EDIT : How do you show and hide the picker? Your code anly shows variable declarations and the delegate methods... answers could vary accordingly..
Since you show the picker as text field's input view, set UITextFieldDelegate for each of these text fields .. and in the textFieldDidBeginEditing check which field becomes active with simple if else
func textFieldDidBeginEditing(textField: UITextField) {
if textField === enterSeason {
activeField = season
}
else if textField === enterSport {
activeField = sport
}
}
And in the picker selector, set value of the relevant text field as per current activeField object
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if activeField === season {
enterSeason.text = season[row] as! String
}
else if activeField === sport {
enterSeason.text = sport[row] as! String
}
}
Setting the delegate for your text fields in storboard/xib :
P.S.
- Rename activeField to activeDataArray or somethiong more appropriate
EDIT 2 : As you mentioned, second approach i have mentioned below is not suitable for you because there are too many of these fields i am still keeping it as part of the answer as it may help someone else
But what you are trying to achieve is very simple and approach is too convoluted / weird. So heres another way you can implement the whole thing..
The easiest (but still probably not the best) way is to have two instances of the UIPickerView for each field. you can directly check pickerView == seasonPickerView OR pickerView == sportPickerViewin an if else block and do the conditional programming and you wont need the activeField variable..