1

我正在以编程方式创建UIPickerView,UIToolbar和。我已将自定义选择器设置为 textView 的 inputView,除了完成按钮外,一切正常。UIBarButtonItemUIButton

有谁知道问题是什么?

我使用的代码是:

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

但完成按钮不可点击。

4

3 回答 3

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

试试这样..

于 2013-10-29T09:52:35.863 回答
1

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

于 2013-10-29T09:34:55.370 回答
0

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

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];
于 2013-10-29T10:13:47.340 回答