這段代碼會滑出一個選擇器視圖鍵盤和附着在它的上面一個完成按鈕。基本上,你想用你的輸入欄設置一個inputAccessoryView。 您應該在輸入字段的觸發事件中調用此方法。
- (IBAction)showYourPicker:(id)sender {
// create a UIPicker view as a custom keyboard view
UIPickerView* pickerView = [[UIPickerView alloc] init];
[pickerView sizeToFit];
pickerView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
self.yourPickerView = pickerView; //UIPickerView
yourTextField.inputView = pickerView;
// create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
// Prepare done button
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered target:self
action:@selector(pickerDoneClicked:)] autorelease];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, nil]];
// Plug the keyboardDoneButtonView into the text field...
yourTextField.inputAccessoryView = keyboardDoneButtonView;
[pickerView release];
[keyboardDoneButtonView release];
}
最後,你完成按鈕調用「pickerDoneClicked」的方法,你應該添加 [yourTextField resignFirstResponder];
這將隱藏選擇器視圖。
flexspace的目的是什麼?即使沒有它,它似乎也能工作?對於評論舊線索感到抱歉。 – KVISH 2012-10-26 00:11:45
@KVISH彈性空間將完成按鈕刷新到右側 – 2013-03-29 11:24:50
我在這裏沒有看到與UIPickerView的連接。 – VsMaX 2016-05-28 20:21:01