Search code examples
iosobjective-cbuttonuipickerview

PickerView is not dismissing when Done button is pressed


I have created a text field that upon entry will open a picker view with a toolbar that contains a done button. However, when the done button is pressed the picker view doesn't dismiss. Everything else works just as I want except this. I've tried several options to no avail. Please review and let me know what I'm missing.

My code is below:

ViewController.h

    #import <UIKit/UIKit.h>

    @interface ViewController : UIViewController

{IBOutlet UITextField *productDescription; IBOutlet UIPickerView *productPicker; NSArray *productListArray}

ViewController.m

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController

    -(void)addPickerView{
productListArray = [[NSArray alloc]initWithObjects:
                    @"myArray", nil];

    productDescription.delegate = self;
    [self.view addSubview:productDescription];
    [productDescription setPlaceholder:@"Product Description"];
    productPicker = [[UIPickerView alloc]init];
    productPicker.dataSource = self;
    productPicker.delegate = self;
    productPicker.showsSelectionIndicator = YES;
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
                               initWithTitle:@"Done" style:UIBarButtonItemStyleDone
                               target:self action:@selector(resignFirstResponder)];
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
                      CGRectMake(50, 320, 50, 50)];
    [toolBar setBarStyle:UIBarStyleBlackOpaque];
    NSArray *toolbarItems = [NSArray arrayWithObjects:
                         doneButton, nil];
    [toolBar setItems:toolbarItems];
    productDescription.inputView = productPicker;
    productDescription.inputAccessoryView = toolBar;
    }

    - (void)viewDidLoad

    {
    [super viewDidLoad];
    [self addPickerView];
    }

    #pragma mark - Text field delegates

    -(void)textFieldDidBeginEditing:(UITextField *)textField

    {
    ([textField.text isEqualToString:@""]);
    }

    #pragma mark - Picker View Data source

    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
    }
    -(NSInteger)pickerView:(UIPickerView *)pickerView
    numberOfRowsInComponent:(NSInteger)component{
return [productListArray count];
    }

    #pragma mark- Picker View Delegate

    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:
    (NSInteger)row inComponent:(NSInteger)component{
    [productDescription setText:[productListArray objectAtIndex:row]];
    }

    - (void)doneButton:(UIBarButtonItem *)sender{
    NSLog(@"Done Touched");
    [productPicker setHidden:YES];
    }

    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
    (NSInteger)row forComponent:(NSInteger)component{
    return [productListArray objectAtIndex:row];
    }

    @end

Solution

  • .M File

    Xib file in take Textfield and set delegate with connect.

    #import "YourViewController.h"
    
    @interface YourViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
    {
        UIPickerView *productPicker;
        NSArray *productListArray;
        IBOutlet UITextField *productDescription;
    
    }
    @end 
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self addPickerView];
    }
    
    -(void)addPickerView
    {
        productListArray = [[NSArray alloc]initWithObjects:@"myArray",@"Rohit",@"Change",@"Your view", nil];
    
    
        [productDescription setPlaceholder:@"Product Description"];
        productPicker = [[UIPickerView alloc]init];
        productPicker.dataSource = self;
        productPicker.delegate = self;
        productPicker.showsSelectionIndicator = YES;
    
        UIToolbar* toolBar = [[UIToolbar alloc] init];
        toolBar.barStyle = UIBarStyleBlack;
        toolBar.translucent = YES;
        toolBar.tintColor = nil;
        [toolBar sizeToFit];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButton:)];
        [toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];
        productDescription.inputView = productPicker;
        productDescription.inputAccessoryView = toolBar;
    
    }
    
    - (IBAction)doneButton:(id)sender
    {
        NSLog(@"Done Touched");
    
        [productPicker removeFromSuperview];
        [productPicker resignFirstResponder];
        [self.view endEditing:YES];
    }
    
    #pragma mark - Text field delegates
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        productDescription.inputView = productPicker;
    }
    
        #pragma mark - Text field delegates
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        productDescription.inputView = productPicker;
    }
    
    #pragma mark - Picker View Data source
    
    -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
        return 1;
    }
    -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
        return [productListArray count];
    }
    
    #pragma mark- Picker View Delegate
    
    -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
        [productDescription setText:[productListArray objectAtIndex:row]];
    }
    
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
        return [productListArray objectAtIndex:row];
    }
    

    I hope this will help you great.