【问题标题】:UItextfield with pickerview as firstresponder ios带有pickerview的UItextfield作为第一响应者ios
【发布时间】:2016-11-22 08:18:01
【问题描述】:

我有一个 UITextField,点击它时需要显示一个 UIPickerView。此外,在pickerview 中选择的数据将被插入到文本字段中。为了实现它,我现在使用 subhajit 的 SHPickerField

【问题讨论】:

  • 你能显示你的三元代码吗
  • 欢迎来到 StackOverflow。请注意,这不是免费的代码编写服务,但我们渴望帮助其他程序员(和有志者)编写自己的代码。请阅读How to ask a good question 上的帮助主题。之后,请使用您迄今为止编写的代码更新您的问题,以完成您希望完成的任务。
  • 你想知道什么?

标签: ios objective-c iphone uipickerview


【解决方案1】:

我确实创建了一个 UITextField 的子类,它很容易实现和使用。这是 GitHub 链接:

访问https://github.com/subhajitregor/SHPickerFieldExample

请查看示例以了解如何使用。自述文件现在也更新了。

【讨论】:

  • 无法运行项目
  • 如果您使用的是较低版本的 Xcode 而不是 Xcode 8,请将您的目标更改为 iOS 9
【解决方案2】:

试试这个

 let myPicker = UIPickerView()
 @IBOutlet var myText: UITextField!

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    myText.inputView = myPicker

}


 @IBAction func textPiker(_ sender: UITextField) {

    sender.inputView = myPicker
}

【讨论】:

    【解决方案3】:

    借助此代码,您还可以打开多个文本字段。

    .h

    UIPickerView *monthPickerView;
    
    @property (strong, nonatomic)NSArray *monthArray;
    
    @property (retain, nonatomic) IBOutlet UITextField *monthTextField;
    

    .m

    self.monthArray=[[NSArray alloc] initWithObjects:@"one",@"two", nil];
    
    self.monthTextField.delegate=self;
    
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
        [self showPicker:textField];
    
     else if(textField==_monthTextField){
        if(self.monthTextField.text.length>=1){
            [monthPickerView selectRow:[self.monthArray indexOfObject:self.monthTextField.text] inComponent:0 animated:NO];
        }
    }
    
    return YES;
    }
    
    
    //**************show picker**************
    
    - (IBAction)showPicker:(id)sender {
    UITextField *textField=(UITextField *)sender;
    monthPickerView = [[UIPickerView alloc] init];
    monthPickerView.showsSelectionIndicator = YES;
    monthPickerView.dataSource = self;
    monthPickerView.delegate = self;
    pickerToolbar = [[UIToolbar alloc] init];
    pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
    [pickerToolbar sizeToFit];
    //to make the done button aligned to the right
    UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                   style:UIBarButtonItemStyleDone target:self
                                                                  action:@selector(doneClicked:)];
    [pickerToolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
    
    if(textField==_monthTextField){
        monthPickerView.tag=;
        self.monthTextField.inputView = monthPickerView;
        self.monthTextField.inputAccessoryView = pickerToolbar;
    }
    

    }

    -(void)doneClicked:(id) sender
    {
    [self.view endEditing:YES];
    
    
    }
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
    {
        return 1;
    }
    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    {
    
    if(pickerView.tag==1){
        self.monthTextField.text = [_monthArray objectAtIndex:row];
    }
    
    
    }
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
    {
    
    if(pickerView.tag==1){
        return [_monthArray count];
    }
    return 0;
    }
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
    {
    
    if(pickerView.tag==1){
        return [_monthArray objectAtIndex:row];
    }
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多