UIPickerview 完成按钮不起作用,即使它没有调用选择器方法
Posted
技术标签:
【中文标题】UIPickerview 完成按钮不起作用,即使它没有调用选择器方法【英文标题】:UIPickerview done button not working, even its not calling selector method 【发布时间】:2016-01-29 10:00:43 【问题描述】:我正在创建UIPickerView
programatically
并添加了一个自定义button
,当用户按下button
时,picker
将不会显示。但是当我按下button
时,它甚至没有调用它的selector
method
。我知道这个问题在这个网站上有很多答案,但它并没有解决我的问题。
-(void) funtion
self.myPickerView = [[UIPickerView alloc]init];
self.myPickerView.dataSource = self;
self.myPickerView.delegate = self;
self.myPickerView.showsSelectionIndicator = YES;
self.myPickerView.frame = CGRectMake(0, 417, 320, 151);
[self.myPickerView setBackgroundColor:[UIColor whiteColor]];
UIToolbar *toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(donePressed:)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[self.myPickerView addSubview:toolBar];
[self.view addSubview:self.myPickerView];
-(void)donePressed:(id)sender
[self.myPickerView endEditing:YES];
【问题讨论】:
你在哪里调用这个方法函数 @Anbu.Karthik 它在 viewdidload 中 您将此选择器视图用于文本字段或其他地方的位置 我在一个按钮上使用它,当我按下那个按钮时,会显示pickerview 【参考方案1】:例如我添加这个
第一步
创建一个 UIPicker
,UItextField
和 NSArray 以加载详细信息
@interface ViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
UITextField *myTextField;
UIPickerView *myPickerView;
NSArray *pickerArray;
@end
第二步
在您的ViewDidLoad
上调用选择器方法,如
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addPickerView];
第三步
create the picker, textfield and done button
-(void)addPickerView
pickerArray = [[NSArray alloc]initWithObjects:@"Chess",
@"Cricket",@"Football",@"Tennis",@"Volleyball", nil];
myTextField = [[UITextField alloc]initWithFrame:
CGRectMake(10, 100, 300, 30)];
myTextField.borderStyle = UITextBorderStyleRoundedRect;
myTextField.textAlignment = NSTextAlignmentCenter;
myTextField.delegate = self;
[self.view addSubview:myTextField];
[myTextField setPlaceholder:@"Pick a Sport"];
myPickerView = [[UIPickerView alloc]init];
myPickerView.dataSource = self;
myPickerView.delegate = self;
myPickerView.showsSelectionIndicator = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithTitle:@"Done" style:UIBarButtonItemStyleDone
target:self action:@selector(done:)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:
CGRectMake(0, self.view.frame.size.height-
myPickerView.frame.size.height-50, 320, 50)];
[toolBar setBarStyle:UIBarStyleBlackOpaque];
NSArray *toolbarItems = [NSArray arrayWithObjects:
doneButton, nil];
[toolBar setItems:toolbarItems];
myTextField.inputView = myPickerView;
myTextField.inputAccessoryView = toolBar;
第四步
如果用户按下完成按钮退出选择器
-(void)done:(id)sender
[myTextField resignFirstResponder];
第五步
PickerView 委托方法
#pragma mark - Picker View Data source
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
return 1;
-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
return [pickerArray count];
#pragma mark- Picker View Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:
(NSInteger)row inComponent:(NSInteger)component
[myTextField setText:[pickerArray objectAtIndex:row]];
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:
(NSInteger)row forComponent:(NSInteger)component
return [pickerArray objectAtIndex:row];
这里我附上了sample project
样例输出
更新
如果你使用隐藏pickerview的按钮使用这个
-(void)donePressed:(id)sender
[self.myPickerView removeFromSuperview];
当你按下按钮时调用这个方法
[self funtion];
【讨论】:
我面临的问题是工具栏放置在pickerview后面,这就是它没有检测到触摸事件的原因。你的解决方案帮助了我。谢谢 这里的关键是将选择器设置为inputView
,将工具栏设置为inputAccessoryView
。不要将工具栏设置为选取器的子视图。
按下完成后,[myPickerView removeFromSuperview]
无法正常工作。我尝试了[self.view endEditing:YES]
,现在可以了!【参考方案2】:
像这样添加完成按钮:-
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
doneButton.frame = CGRectMake(0, 0, 40, 50);
[doneButton addTarget:self action:@selector(donePressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithCustomView:logoButton];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[self.myPickerView addSubview:toolBar];
【讨论】:
【参考方案3】:将工具栏作为 inputAccessoryView 添加到文本字段,
// Tool bar
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,35)];
pickerToolbar.barStyle = UIBarStyleDefault;
// Bar button
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneButtonTapped:)];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[pickerToolbar setItems:@[flexSpace,doneButton]];
self.selectedLockerTextFiled.inputAccessoryView = pickerToolbar;
self.selectedLockerTextFiled.inputView = picker;
【讨论】:
以上是关于UIPickerview 完成按钮不起作用,即使它没有调用选择器方法的主要内容,如果未能解决你的问题,请参考以下文章
Objective C 使用“完成”按钮实现 UIPickerView
Swift - UIPickerView 子视图中没有出现工具栏完成按钮