1

我的应用程序中有一个类似表单的视图,其中包含 8 个文本字段。其中两个由选择器填写,其余的由正常的草板打字。When picker-row is selected or when return button is pressed, the corresponding text should be entered into the textfield.

您能否提供一些建议如何正确实施?也使得选择器/键盘在选择/按下后被关闭。

我在这里找到了类似问题的答案。并且我设法让 2 个选择器文本字段正常工作,但现在我无法让键盘出现在其余部分。我想这是因为我重写了 textFieldShouldBegineEditing 方法。有什么想法可以解决这个问题吗?我可以以某种方式从 textFieldShouldBeginEditing 内部调用默认方法吗?请注意,如果我取消注释我最后几行 textFieldShouldBeginEditing 我会崩溃...

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

if (textField == self.pickerField1) {
    currentArray = self.array1;
    [pickerView reloadAllComponents];
    pickerView.hidden = NO;
    [self animatePickerViewIn];
    return NO;
}
if (textField == self.pickerField2){
    currentArray = self.array2;
    [pickerView reloadAllComponents];
    pickerView.hidden = NO;
    [self animatePickerViewIn];
    return NO;

}
//    else  {
//        [currentTextField becomeFirstResponder];
//        return NO;
//    }
- (void)pickerView:(UIPickerView *)pickerView 
  didSelectRow:(NSInteger)row inComponent: (NSInteger)component
{
[currentTextField setText:[currentArray objectAtIndex:row]];
[currentTextField resignFirstResponder];
pickerView.hidden = YES;
}

谢谢!

4

1 回答 1

1

为需要更多信息的任何人添加此内容-这是我使用user2135738提供的代码使其工作的方式:

MyViewController.h

#import <UIKit/UIKit.h>

@interface MyViewController : UIViewController  <UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *aTextField;
@property (weak, nonatomic) IBOutlet UITextField *bTextField;

@end

我的视图控制器.m

#import "MyViewController.h"

@interface MyViewController ()
@end

@implementation MyViewController {
   NSArray *aArray,
   *bArray,
   *currentArray;

   UITextField *currentTextField;
   UIPickerView *pickerView;
}

- (void)viewDidLoad {
   aArray = [NSArray arrayWithObjects: @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18, @19, @20, nil];
   bArray = [NSArray arrayWithObjects:@"aString", @"bString", nil];

   [super viewDidLoad];

   pickerView = [[UIPickerView alloc] init];
   pickerView.dataSource = self;
   pickerView.delegate = self;
   self.aTextField.delegate = self;
   self.bTextField.delegate = self;

   self.aTextField.inputView = pickerView;
   self.bTextField.inputView = pickerView;
}

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

   if (textField == self.aTextField) {
      currentArray = aArray;
      [pickerView reloadAllComponents];
      pickerView.hidden = NO;
      return YES;
   } else if (textField == self.bTextField){
      currentArray = bArray;
      [pickerView reloadAllComponents];
      pickerView.hidden = NO;
      return YES;
   } else  {
      [currentTextField becomeFirstResponder];
      return NO;
   }

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
   return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
   return [currentArray count];
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
   return [NSString stringWithFormat:@"%@",[currentArray objectAtIndex:row]];
}

@end
于 2015-11-29T07:12:31.130 回答