簡體   English   中英

選擇 UITextField 時如何顯示 UIPickerView

[英]How to Show UIPickerView when selecting UITextField

這是我到目前為止所做的截圖:

在此處輸入圖像描述

所以我想做的是當你 select“選擇一個名字”文本字段時,我需要一個 Picker 來顯示,輸入 @“Jack”。

由於 iOS 3.2, UITextField支持inputView屬性來分配自定義視圖以用作鍵盤,這提供了一種顯示UIPickerView的方法:

您可以使用UITextFieldinputView屬性,可能與inputAccessoryView屬性結合使用。 您將您的pickerView分配給inputView屬性,並為關閉選擇器,將完成按鈕分配給inputAccessoryView屬性。

UIPickerView *myPickerView = [[UIPickerView alloc] init];
//myPickerView configuration here...
myTextField.inputView = myPickerView;

像那樣。 這不會為您提供直接關閉視圖的方法,因為您的UIPickerView沒有返回按鈕,這就是為什么我建議使用inputAccessoryView屬性來顯示帶有完成按鈕的工具欄(該欄只是為了美觀,您不妨只需使用UIButton對象):

UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:
 CGRectMake(0,0, 320, 44)]; //should code with variables to support view resizing
UIBarButtonItem *doneButton =
 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
 target:self action:@selector(inputAccessoryViewDidFinish)];
 //using default text field delegate method here, here you could call
 //myTextField.resignFirstResponder to dismiss the views
[myToolbar setItems:[NSArray arrayWithObject: doneButton] animated:NO];
myTextField.inputAccessoryView = myToolbar;

我使用它並發現這比添加子視圖和動畫UIPicker干凈得多

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
responder = textField;

    if ([textField isEqual:self.txtBirthday]) {
    UIDatePicker *datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
    [datepicker setDatePickerMode:UIDatePickerModeDate];

    textField.inputView = datepicker;
    }

    return YES;
}

它會為你工作..我已經編輯了它。為此你必須為文本字段設置委托。 並在 NIb 文件中創建一個 UIPIckrView。

- (BOOL) textFieldShouldBeginEditing:(UITextView *)textView
{
    pickrView.frame = CGRectMake(0, 500, pickrView.frame.size.width,    pickrView.frame.size.height);
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.50];
    [UIView setAnimationDelegate:self];
    pickrView.frame = CGRectMake(0, 200, pickrView.frame.size.width, pickrView.frame.size.height);
    [self.view addSubview:pickrView];
    [UIView commitAnimations];
    return NO;
}

好吧,您可以依靠UITextFieldDelegate來處理這種功能。

在 - 的里面

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

是您設置當前UITextField的文本以及初始化和顯示UIPickerView的地方。

重要通知:

您可能還想遵守UIPickerViewDelegate

高溫高壓

Swift:

internal var textFieldHandlerToolBar: UIToolbar = {
    let tb = UIToolbar.init(frame: CGRect.init(origin: .zero, size: CGSize.init(width: UIScreen.main.bounds.width, height: 44.0)))
    let doneBarButton = UIBarButtonItem.init(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(actionDonePickerSelection))
    tb.setItems([doneBarButton], animated: false)
    return tb
}()

internal var pickerView: UIPickerView = {
    let pv = UIPickerView.init()
    return pv
}()

@objc internal func actionDonePickerSelection() {
     textField.resignFirstResponder()
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.pickerView.delegate = self
    self.pickerView.datasource = self
}

像這樣使用它:

textField.inputAccessoryView = self.textFieldHandlerToolBar
textField.inputView = self.pickerView

http://tmblr.co/ZjkSZteCOUBS

我有代碼和我博客中列出的所有內容來准確地做到這一點。 但在下面,我列出了基本概念。

基本上,該解決方案涉及在 github 上名為 ActionSheetPicker 的開源項目,並在UITextFieldDelegate上實現 function textFieldShouldBeginEditing 您可以在那里關閉鍵盤並提供 UIPickerView 。 這里列出了基本代碼:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    // We are now showing the UIPickerViewer instead

    // Close the keypad if it is showing
    [self.superview endEditing:YES];

    // Function to show the picker view
    [self showPickerViewer :array :pickerTitle];

    // Return no so that no cursor is shown in the text box
    return  NO;
}

視圖控制器.h

@interface ChangeCurrencyVC : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
      NSArray *availableCurreniesArray;
}
@property (weak, nonatomic) IBOutlet UITextField *chooseCurrencyTxtFldRef;

視圖控制器.m

 - (void)viewDidLoad {
[super viewDidLoad];
availableCurreniesArray = @[@"Indian Rupee", @"US Dollar", @"European Union Euro", @"Canadian Dollar", @"Australian Dollar", @"Singapore Dollar", @"British Pound", @"Japanese Yen"];
// Do any additional setup after loading the view.
[self pickerview:self];
}

 #pragma mark -  picker view Custom Method
 -(void)pickerview:(id)sender{
  UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;

// set change the inputView (default is keyboard) to UIPickerView
self.chooseCurrencyTxtFldRef.inputView = pickerView;

// add a toolbar with Cancel & Done button
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTouched:)];
// the middle button is to make the Done button align to right
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
self.chooseCurrencyTxtFldRef.inputAccessoryView = toolBar;
}
 #pragma mark - doneTouched
- (void)cancelTouched:(UIBarButtonItem *)sender{
// hide the picker view
[self.chooseCurrencyTxtFldRef resignFirstResponder];
 }
  #pragma mark - doneTouched
- (void)doneTouched:(UIBarButtonItem *)sender{
// hide the picker view
[self.chooseCurrencyTxtFldRef resignFirstResponder];
// perform some action
 }
#pragma mark - The Picker Challenge
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [availableCurreniesArray count];
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent:(NSInteger)component{
return availableCurreniesArray[row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.chooseCurrencyTxtFldRef.text = availableCurreniesArray[row];
}

您可以做的是,在UIButton上創建一個具有自定義類型的UITextField 兩者具有相同的大小。 輕觸按鈕,您可以顯示UIPickerView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM