UIPickerview完成按钮不起作用,甚至它不调用选择器方法

时间:2016-01-29 10:00:44

标签: ios objective-c uipickerview

我正在创建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];
}

3 个答案:

答案 0 :(得分:3)

例如我添加此

<强>步骤1

创建一个UIPickerUItextField和NSArray以加载详细信息

@interface ViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
{

UITextField *myTextField;
UIPickerView *myPickerView;
NSArray *pickerArray;
}

@end

<强>步骤-2

ViewDidLoad上调用选择器方法

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self addPickerView];
}

<强>步骤-3

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;

}

<强>步骤4

如果用户按下完成按钮,则重新选择选择器

-(void)done:(id)sender
{

[myTextField resignFirstResponder];
}

<强>步骤5

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

示例输出

enter image description here

<强>更新

如果您使用了隐藏的按钮,则选择器视图使用此

-(void)donePressed:(id)sender
{
  [self.myPickerView removeFromSuperview];

}

按下按钮时调用此方法

[self funtion];

答案 1 :(得分:0)

像这样添加完成按钮: -

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

答案 2 :(得分:0)

将工具栏作为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;