Iphone “完成”按钮在编程UIBarButtonim和UIPickerView上不工作

Iphone “完成”按钮在编程UIBarButtonim和UIPickerView上不工作,iphone,objective-c,uipickerview,Iphone,Objective C,Uipickerview,我正在以编程方式创建一个UIPickerView,UIToolbar,uibarbuttoneim和UIButton。我将自定义选取器设置为textView的inputView,除“完成”按钮外,其他一切都可以正常工作 有人知道问题出在哪里吗 我使用的代码是: // initialize picker CGRect cgRect =[[UIScreen mainScreen] bounds]; CGSize cgSize = cgRect.size; _picker = [[UIPickerV

我正在以编程方式创建一个
UIPickerView
UIToolbar
uibarbuttoneim
UIButton
。我将自定义选取器设置为textView的inputView,除“完成”按钮外,其他一切都可以正常工作

有人知道问题出在哪里吗

我使用的代码是:

// initialize picker
CGRect cgRect =[[UIScreen mainScreen] bounds];
CGSize cgSize = cgRect.size;

_picker = [[UIPickerView alloc] init];
_picker.frame=CGRectMake(0, 0, cgSize.width, cgSize.height);
_picker.showsSelectionIndicator = YES;
_picker.delegate = self;

// toolbar of picker
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0, 0, cgSize.width, 35);
toolbar.barStyle = UIBarStyleBlackTranslucent;

UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIButton *customButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 33)];
[customButton setTitle:@"Done" forState:UIControlStateNormal];
[customButton addTarget:self action:@selector(doneClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];

NSMutableArray * arr = [NSMutableArray arrayWithObjects:flexibleSpaceLeft, barCustomButton, nil];
[toolbar setItems:arr animated:YES];
self.txtTeam.inputView = _picker;
[_picker addSubview:toolbar];
doneClicked
方法是

    -(void)doneClicked
{
    NSLog(@"Done button clicked.");
    [self.txtTeam resignFirstResponder];
}

但“完成”按钮不可单击

您已将按钮的选择器设置为调用doneClicked:但该方法称为doneClicked。从选择器中方法名称的末尾删除:,它应该可以工作。

您已将按钮的选择器设置为调用doneClicked:但该方法称为doneClicked。在选择器中,从方法名称的末尾删除:应该可以工作

UIButton *customButton =  [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setFrame:CGRectMake(0, 0, 30, 60)];
[customButton setTitle:@"Done" forState:UIControlStateNormal];
[customButton addTarget:self action:@selector(doneClicked:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
像这样试试


尝试这样做。

将代码更改为以下内容:-

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(0, 0, 60, 33);
[customButton addTarget:self action:@selector(doneClicked) forControlEvents:UIControlEventTouchUpInside];
customButton.showsTouchWhenHighlighted = YES;
[customButton setTitle:@"Done" forState:UIControlStateNormal];
customButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
只需在代码下方添加注释,然后在上面进行修改

   // UIButton *customButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 33)];
   // [customButton setTitle:@"Done" forState:UIControlStateNormal];
    //[customButton addTarget:self action:@selector(doneClicked:) forControlEvents:UIControlEventTouchUpInside];
   // UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];

将代码更改为以下内容:-

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
customButton.frame = CGRectMake(0, 0, 60, 33);
[customButton addTarget:self action:@selector(doneClicked) forControlEvents:UIControlEventTouchUpInside];
customButton.showsTouchWhenHighlighted = YES;
[customButton setTitle:@"Done" forState:UIControlStateNormal];
customButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
只需在代码下方添加注释,然后在上面进行修改

   // UIButton *customButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 33)];
   // [customButton setTitle:@"Done" forState:UIControlStateNormal];
    //[customButton addTarget:self action:@selector(doneClicked:) forControlEvents:UIControlEventTouchUpInside];
   // UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];

使用了以下代码,但不起作用。[customButton addTarget:self action:@selector(Doneclick)for ControlEvents:UIControlEventTouchUpInside];如果使用@TamilKing中的示例中的buttonWithType方法,是否有效?例如,使用buttonWithType方法和选择器(doneClicked)创建按钮,而不使用:。使用了以下代码,但不起作用。[customButton addTarget:self action:@selector(Doneclick)for ControlEvents:UIControlEventTouchUpInside];如果使用@TamilKing中的示例中的buttonWithType方法,是否有效?例如,使用buttonWithType方法和选择器(doneClicked)创建按钮,但不使用:。请尝试我的代码。使用customTry my代码创建按钮。使用Custom创建按钮我按照您的建议更改了方法,但仍然不起作用。我当前的方法是:-(iAction)doneClicked:(id)sender{[self.txtTeam resignFirstResponder];}我当前的代码是:您的代码+NSMutableArray*arr=[NSMutableArray arrayWithObjects:flexibleSpaceLeft,barCustomButton,nil];[工具栏设置项:arr动画:是];[_选取器添加子视图:工具栏];self.txtTeam.inputView=\u选择器;仍然不起作用。基本上,在您编写代码的地方,我认为您写错了地方。请检查??Ohk no it is right只包括自定义按钮而不添加到barCustomButton,并测试它是否仍在调用??是否调用此[super viewDidLoad];在您的viewdidload中??我按照您的建议更改了方法,但仍然不起作用。我当前的方法是:-(iAction)doneClicked:(id)sender{[self.txtTeam resignFirstResponder];}我当前的代码是:您的代码+NSMutableArray*arr=[NSMutableArray arrayWithObjects:flexibleSpaceLeft,barCustomButton,nil];[工具栏设置项:arr动画:是];[_选取器添加子视图:工具栏];self.txtTeam.inputView=\u选择器;仍然不起作用。基本上,在您编写代码的地方,我认为您写错了地方。请检查??Ohk no it is right只包括自定义按钮而不添加到barCustomButton,并测试它是否仍在调用??是否调用此[super viewDidLoad];在你的视图中加载??