我所拥有的是3个textFields,每当我触摸textField1或textField2时,Picker会更改值并且没有键盘。当我触摸textField3键盘出现并且选择器被隐藏。现在,如果我通过单击return关闭键盘然后再次单击textField1选择器,但如果我不解除键盘按下单击按钮它将保持在选择器上方。我需要的是当键盘是firstResponder(我在屏幕上看到它)时隐藏它,如果我点击textField1并且只看到选择器
int variabla;
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[pickerView setHidden:YES];
if (textField1.editing == YES) {
[textField1 resignFirstResponder];
[pickerView setHidden:NO];
variabla = 1;
}else if (textField2.editing == YES) {
[textField2 resignFirstResponder];
[pickerView setHidden:NO];
variabla = 2;
}
NSLog(@"variabla %d",variabla);
[pickerView reloadAllComponents];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if (variabla == 1) {
return [pickerArray1 count];
}else if (variabla == 2) {
return [pickerArray2 count];
}else {
return 0;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (variabla == 1) {
return [pickerArray1 objectAtIndex:row];
}else if (variabla == 2) {
return [pickerArray2 objectAtIndex:row];
}else {
return 0;
}
}
- (void)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
}
- (void)viewDidLoad {
[super viewDidLoad];
[pickerView setHidden:YES];
pickerArray1 = [[NSMutableArray alloc] initWithObjects:@"0", @"1", @"2", nil];
pickerArray2 = [[NSMutableArray alloc] initWithObjects:@"3", @"4", @"5", nil];
}
答案 0 :(得分:2)
好的解决这个问题你可以使用很多选项我告诉你其中一个。
- (void)textFieldShouldBeginEditing:(UITextField *)textField {
if([textField isEqual:yourTextField3])
{
yourPicker.hidden=YES;
}
else
{
[yourTextField3 resignFirstResponder];
[self performSelector:@selector(textFieldFirstResponderOnDelay) withObject:nil afterDelay:0.3];
}
}
- (void)textFieldFirstResponderOnDelay {
yourPicker.hidden=NO;
}
无论你想要什么,你都可以适应这些条件。
或者您可以在文本字段中添加标签并进行相应检查。随时随地隐藏选择器或显示选择器并随时隐藏键盘。
答案 1 :(得分:0)
@implementation UIPickerViewController
int variabla;
-(void)textFieldDidBeginEditing:(UITextField *)textField {
if([textField isEqual:textField3])
{
pickerView.hidden=YES;
}
else
{
pickerView.hidden=NO;
[textField3 resignFirstResponder];
if (textField1.editing == YES) {
variabla = 1;
}else if (textField2.editing == YES) {
variabla = 2;
}
NSLog(@"variabla %d",variabla);
[pickerView reloadAllComponents];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
if (variabla == 1) {
return [pickerArray1 count];
}else if (variabla == 2) {
return [pickerArray2 count];
}else {
return 0;
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (variabla == 1) {
return [pickerArray1 objectAtIndex:row];
}else if (variabla == 2) {
return [pickerArray2 objectAtIndex:row];
}else {
return 0;
}
}
- (BOOL)textFieldShouldReturn:(UITextField
*)textField{
[textField resignFirstResponder];
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
[pickerView setHidden:YES];
pickerArray1 = [[NSMutableArray alloc] initWithObjects:@"0", @"1", @"2", nil];
pickerArray2 = [[NSMutableArray alloc] initWithObjects:@"3", @"4", @"5", nil];
}
和.h文件
@interface UIPickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIPickerView *pickerView;
IBOutlet UITextField *textField1;
IBOutlet UITextField *textField2;
IBOutlet UITextField *textField3;
NSArray *pickerArray1;
NSArray *pickerArray2;
UIButton *hideKeyboardButton;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField;
@end
答案 2 :(得分:0)
得到你的帮助
-(void)textFieldShouldBeginEditing:(UITextField *)textField {
pickerView.hidden=YES;
if ([textField isEqual:textField3]) {
}
else if([textField isEqual:textField1])
{
[textField3 resignFirstResponder];
[self performSelector:@selector(textFieldFirstResponderOnDelay1) withObject:nil afterDelay:0.1];
}else {
[textField3 resignFirstResponder];
[self performSelector:@selector(textFieldFirstResponderOnDelay2) withObject:nil afterDelay:0.1];
}
}
-(void)textFieldFirstResponderOnDelay1{
pickerView.hidden=NO;
variabla = 1;
NSLog(@"variabla %d",variabla);
[pickerView reloadAllComponents];
}
-(void)textFieldFirstResponderOnDelay2{
pickerView.hidden=NO;
variabla = 2;
NSLog(@"variabla %d",variabla);
[pickerView reloadAllComponents];
}
选择器必须在触摸textField3
之前初始化