Uipickerview中的完成按钮无效

时间:2014-11-13 03:54:00

标签: ios objective-c uipickerview

我想在uipicker视图中添加完成按钮,以便在文本字段中获取选择器视图的选定值,我尝试过以下代码,但它不起作用。当我创建barbutton名称'完成'并对其执行自我操作,该功能未触发以下是我的代码,请审查它。 Thanx提前。

- (IBAction)selectbtn:(id)sender {
    pickerview = [[UIPickerView alloc] initWithFrame:CGRectMake(5, 40, 300, 300)];
    pickerview.showsSelectionIndicator = YES;
    pickerview.hidden = NO;
    pickerview.delegate = self;
    UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    toolBar.barStyle = UIBarStyleBlackOpaque;

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];

    [toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];
    self.Textbox.inputAccessoryView=toolBar;
    [pickerview addSubview:toolBar];

    [self.view addSubview:pickerview];

}
- (void)doneTouched:(UIBarButtonItem *)sender // this function is not triggering
{
    // hide the picker view
    [self.Textbox resignFirstResponder]; // 


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


}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.jsonresultarr.count;


}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{

//    return [[self.jsonresultarr objectAtIndex:row] objectForKey:@"Company_Id"];
  //  NSObject *companyId = [[self.jsonresultarr objectAtIndex:row] objectForKey:@"Company_Id"];
    NSObject *companyName = [[self.jsonresultarr objectAtIndex:row] objectForKey:@"Company_Name"];
    return [NSString stringWithFormat:@"%@", companyName, nil];




}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.Textbox.text=[[self.jsonresultarr objectAtIndex:row] objectForKey:@"Company_Name"];
    [self.pickerview removeFromSuperview];


}

1 个答案:

答案 0 :(得分:0)

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched)];

[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];
self.Textbox.inputAccessoryView=toolBar;


- (void)doneTouched   
{

 NSLog (@"result");
[self.Textbox resignFirstResponder];  


}