我試圖打開UIPickerView
作爲inputView到UITextfield
。如何將UIPickerView設置爲inputView到iPhone中的UITextField
這是我在textFieldDidBeginEditing
代碼:
UIPickerView *categoryPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 43, 320, 480)];
categoryPicker.delegate = self;
categoryPicker.dataSource = self;
[categoryPicker setShowsSelectionIndicator:YES];
txtCategory.inputView = categoryPicker;
UIToolbar *_providerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
_providerToolbar.barStyle = UIBarStyleBlackOpaque;
[_providerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissActionSheet)];
[barItems addObject:doneBtn];
[_providerToolbar setItems:barItems animated:YES];
txtCategory.inputAccessoryView = _providerToolbar;
它的正常工作的第一次,但是當我從UIPickerView
選擇任意值,它的隱藏和我UITextfield
是編輯和UIPickerView不當時顯示。我究竟做錯了什麼?
我不會把這些代碼在'textFieldDidBeginEditing:',該方法被調用後'UITextField'已經成爲第一個響應者,可能是什麼原因造成您的問題。嘗試將此代碼移至'viewDidLoad'。 –