我的应用程序中有一个类似于窗体的视图,其中包含8个文本字段。其中两个将由拣货员填写,其余的由正常的heyboard打字。选择选取行或按下返回按钮时,应将相应的文本输入文本字段。
请您提供一些如何正确实施的建议?此外,选择/返回后,选择器/键盘将被解除。
我找到了类似here之类的答案。我设法让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;
}
谢谢!
答案 0 :(得分: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
<强> MyViewController.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