按下“完成”按钮时 PickerView 不会关闭

PickerView is not dismissing when Done button is pressed

我创建了一个文本字段,输入后将打开一个带有包含完成按钮的工具栏的选择器视图。但是,当按下完成按钮时,选择器视图不会关闭。除了这个,其他一切都按我想要的方式工作。我尝试了几种选择都无济于事。请检查并让我知道我遗漏了什么。

我的代码如下:

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

.M File

Xib 文件中的文本字段并使用连接设置委托。

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

希望对你有帮助。