UIPicker子视图无法识别输入

时间:2014-02-18 17:45:50

标签: ios objective-c uipickerview

我正在尝试使用类似于键盘上的inputAccessoryView的工具栏来解雇UIPickerView。

我对我的文本字段和我的选择器使用相同的UIToolbar,文本字段将正确解除,但选择器甚至无​​法识别按钮上的触摸。

这是我用来创建工具栏的代码:

UIToolbar *tipToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
tipToolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *donebtn = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(resignResponder:)];
tipToolbar.items = [NSArray arrayWithObjects:
                    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                    donebtn, nil
];
[tipToolbar sizeToFit];
_outlet_txt_ipaddr.inputAccessoryView = tipToolbar;
[_outlet_picker addSubview:tipToolbar];

这是解除键盘或选择器的动作:

- (IBAction)resignResponder:(id)sender{
    [_outlet_txt_ipaddr resignFirstResponder];
    [_outlet_picker resignFirstResponder];
}

工具栏显示在选择器视图上,但我点击完成按钮甚至没有显示动画,更不用说调用resignResonder操作了。

1 个答案:

答案 0 :(得分:1)

for picker view [_outlet_picker resignFirstResponder];不行。

您需要为选择器视图实现委托。

试试这段代码

UIToolbar *tipToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
tipToolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *donebtn = [[UIBarButtonItem alloc] initWithTitle:@"Done"   style:UIBarButtonItemStyleDone target:self action:@selector(resignResponder:)];
tipToolbar.items = [NSArray arrayWithObjects:
                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                donebtn, nil
 ];
[tipToolbar sizeToFit];
_outlet_txt_ipaddr.inputAccessoryView = tipToolbar;

_outlet_picker.delegate=self;
_outlet_picker.dataSource=self;
_outlet_picker.showsSelectionIndicator=YES;

[self.view addSubview:outlet_picker];

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
 {
return 1;
 }

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

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:pickerValueAry];
id str=[ary objectAtIndex:row];
return str;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{    
NSLog(@"selectedRowInPicker >> %d",row);
}  

您可以在行选择中关闭选择器视图。