我正在以編程方式創建UIPickerView
,UIToolbar
,UIBarButtonItem
和UIButton
。我將自定義選擇器設置爲textView的輸入視圖,除了完成按鈕外,其他所有工作都沒有問題。完成按鈕不能在編程UIBarButtonItem和UIPickerView工作
有沒有人有任何想法是什麼問題?
我使用的代碼是:
// 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];
}
但是完成按鈕不可點擊。
試試我的代碼。使用自定義創建按鈕 – TamilKing