按下完成按钮时,PickerView 不会关闭
Posted
技术标签:
【中文标题】按下完成按钮时,PickerView 不会关闭【英文标题】:PickerView is not dismissing when Done button is pressed 【发布时间】:2015-09-30 21:00:03 【问题描述】:我创建了一个文本字段,输入后将打开一个选择器视图,其中包含一个包含完成按钮的工具栏。但是,当按下完成按钮时,选择器视图不会关闭。除了这个,其他一切都按我的意愿工作。我尝试了几个选项都无济于事。请查看并让我知道我缺少什么。
我的代码如下:
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
【问题讨论】:
- (void)doneButton:(UIBarButtonItem *)sender
方法在哪里调用?
你试过在 ViewController.h 上添加 .M 文件
Xib 文件采用 Textfield 并通过连接设置委托。
#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];
我希望这会对你有所帮助。
【讨论】:
这工作得很好,我确实看到了我所做的错误。感谢您修复,我正在路上看看我还能在这个项目中搞砸什么!以上是关于按下完成按钮时,PickerView 不会关闭的主要内容,如果未能解决你的问题,请参考以下文章
按下覆盖按钮时不会关闭 imagePickerViewController
Pickerview 在文本字段中第二次单击时显示为空,当我按下完成或取消应用程序崩溃时