Done Button不适用于程序化UIBarButtonItem和UIPickerView

时间:2013-10-29 09:32:03

标签: iphone objective-c uipickerview

我正在以编程方式创建UIPickerViewUIToolbarUIBarButtonItemUIButton。我将自定义选择器设置为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];
}

但完成按钮无法点击。

3 个答案:

答案 0 :(得分: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];

尝试这样..

答案 1 :(得分:1)

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

答案 2 :(得分: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];