How to Show Uipickerview When Selecting Uitextfield

How to Show UIPickerView when selecting UITextField

it will work for you .. i have edited it .and for that you have to set delegate for textfield. and create a UIPIckrView in NIb file.

- (BOOL) textFieldShouldBeginEditing:(UITextView *)textView
{
pickrView.frame = CGRectMake(0, 500, pickrView.frame.size.width, pickrView.frame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.50];
[UIView setAnimationDelegate:self];
pickrView.frame = CGRectMake(0, 200, pickrView.frame.size.width, pickrView.frame.size.height);
[self.view addSubview:pickrView];
[UIView commitAnimations];
return NO;
}

Show UIPickerView text field is selected, then hide after selected

If I understood well your question, you want:

  1. Have an UITextField which display a text selected
  2. Opening a picker when the user click on the UITextField
  3. Close the picker when an item (in the picker) is selected, and set it in the UITextField

This is the complete code to manage it, you just have to link the delegate of your UITextField:

@IBOutlet var textfieldBizCat: UITextField!
@IBOutlet var pickerBizCat: UIPickerView! = UIPickerView()

var bizCat = ["Cat One", "Cat Two", "Cat Three"]

override func viewDidLoad() {
super.viewDidLoad()
pickerBizCat.hidden = true;
textfieldBizCat.text = bizCat[0]
}

// returns the number of 'columns' to display.
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
return 1
}

// returns the # of rows in each component..
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
return bizCat.count
}

func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
return bizCat[row]
}

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
textfieldBizCat.text = bizCat[row]
pickerBizCat.hidden = true;
}

func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
pickerBizCat.hidden = false
return false
}

What I changed from your code:

  • Used UITextFieldDelegate to display the picker when the UITextField is selected
  • Hide the picker once an item is selected, and setup the UITextField
  • Set the first row of your picker in the UITextField when any item is selected

How to display information in UIPickerView when clicking on UITextField

It seems, that nowhere in your code you set the delegate and data source of the UIPickerView. Without them, the picker view doesn't know what to display and how to inform you that the user has selected something.

Please take a look here : UIPickerViewDataSource and here UIPickerViewDelegate and let me know if something is not clear or you run into trouble implementing these.


Here is an example :

@interface ViewController () <UIPickerViewDelegate, UIPickerViewDataSource> //#1

@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *pickerNames;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

self.pickerView = [[UIPickerView alloc] init];
self.pickerView.delegate = self; //#2
self.pickerView.dataSource = self; //#2

self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.frame), 50)];
self.textField.borderStyle = UITextBorderStyleRoundedRect;
self.textField.placeholder = @"What's your favourite programming language?";
self.textField.inputView = self.pickerView;

self.pickerNames = @[ @"Objective-C", @"Swift", @"Java", @"C", @"C++", @"Other"];

[self.view addSubview:self.textField];
}

#pragma mark - UIPickerViewDataSource

// #3
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
if (pickerView == self.pickerView) {
return 1;
}

return 0;
}

// #4
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView == self.pickerView) {
return [self.pickerNames count];
}

return 0;
}

#pragma mark - UIPickerViewDelegate

// #5
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (pickerView == self.pickerView) {
return self.pickerNames[row];
}

return nil;
}

// #6
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (pickerView == self.pickerView) {
self.textField.text = self.pickerNames[row];
}
}

@end

Okay, here are the interesting parts :

  1. Here we declare that we will be conforming to UIPickerViewDelegate and UIPickerViewDataSource protocols. A protocol is in its basic a set of method headers. When you declare that you will conform to a protocol, you need to implement them (those that are required at least).

  2. This is quite obvious, we tell the picker that we will be its delegate and datasource. If we didn't declare conformance to the protocols before, the compiler would start giving us errors here.


    1. This method from UIPickerViewDataSource will be called to know how many components should be displayed. A date picker has typicaly 3 components (day, month, year) for example.

    2. This method wants to know how many elements there will be in every component.

    3. This method is where you actually provide the texts to display to the user. You could also implement one of the other two variations of this method to provide either an attributed text or a totally custom view.

    4. This method gets called when the animation stops on a new item.

The checks for pickerView == self.pickerView are not needed in this example. I have added them, so that you'd know how to decide for which picker (as you have 4 it seems) to return the information needed. Ideally, this would need some further refactoring, as it is not the best idea to have cluttered delegates/data sources.

UIPickerView is not selecting the date in UITextField

you miss few things in view did load I had updated the code

override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate=self
pickerView.dataSource=self
genderTextField.inputView = pickerView
genderTextField.delegate = self
dateTextField.delegate = self

}

Assign the text field delegate and implement the function textFieldShouldBeginEditing function for showing the Date Picker

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if textField.tag == 1 { //Date Text Field
showDatePicker()
}
return true
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}

I had assigned the tag value of Date Text Field from the storyboard and hide the picker view from the storyboard. It will solve your both points

Filling a UITextField with a UIPickerView option immediately

You need to implement textFieldShouldBeginEditing: method.

Set delegate

yourTextfield.delegate = self

Implement delegate method

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

if (textField == yourTextfield) {

// your logic goes here...
yourTextfield.text = prevCardiacIntervPickerOption.first

}

return true
}

How can I set selectRow on a UIPickerView that is attached to a UITextField

enum AvailableColors: Int {
case yellow = 0
case blue = 1
case red = 2
case green = 3

var colorByIndex: UIColor {
switch (self) {
case .yellow:
return .yellow
case .blue:
return .blue
case .red:
return .red
case .green:
return .green
}
}
}

class ViewController: UIViewController, UIPickerViewDelegate {
var picker : UIPickerView?
var selectedIndex = 0

override func viewDidLoad() {
super.viewDidLoad()
//1.init your picker with data.
// self.picker = UIPickerView()...
// self.picker.delegate = self
//2.select predefined index
self.picker?.selectRow(self.selectedIndex, inComponent: 0, animated: false)

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.selectedIndex = row
self.textField.backgroundColor = AvailableColors(rawValue: row)?.colorByIndex
}
}

UIPickerView as an inputView of multiple UITextFields

on your textfield delegate assign the inputview and tag for your textfield

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField.inputView = pcv
pcv.tag = textField.tag
return true;
}

and finally get the tag for pickerview for identify which textfield you tapped.

 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("pickerView == \(pickerView.tag)")

}


Related Topics



Leave a reply



Submit