[objective-c]做按钮在 Uipicker 视图不工作

标签: ios Objective-C
发布时间: 2016/10/2 0:30:53
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要添加完成的按钮在 uipicker 视图中,即可在文本字段中获取所选的值的选取器视图我试过下面的代码,但它不工作。在创建按钮名称时 ' 做 ',对它的功能不触发执行自我行动下面是我慈祥的代码请查看它。提前谢谢。

- (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:

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


}
官方微信
官方QQ群
31647020