UIPicker 和键盘冲突

发布于 2024-10-06 04:39:20 字数 1927 浏览 2 评论 0原文

我拥有的是 3 个文本字段,每当我触摸 textField1 或 textField2 选择器都会更改值并且没有键盘。当我触摸 textField3 键盘时,会出现键盘并且选择器会隐藏。现在,如果我通过单击“返回”关闭键盘,然后单击 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];
}

what i have is 3 textFields and whenever i touch textField1 or textField2 Picker changes values and there is no keyboard. When i touch textField3 keyboard appears and the picker goes hidden.Now if i dismiss the keyboard by clicking return and then click textField1 picker appears again, but if i dont dismiss the keyboard BY CLICKING BUTTON it stays over the picker. What I need is when the keyboard is firstResponder (and i see it on the screen) to hide it if i click on the textField1 and only to see the picker

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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平定天下 2024-10-13 04:39:20

好的,为了解决这个问题,您可以使用很多选项,我告诉您其中之一。

- (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;
}

无论你想要什么,你都可以在这些条件中加入。

或者您可以在文本字段中添加标签并进行相应的检查。随时隐藏选择器或随时显示选择器并隐藏键盘。

ok for solve this problem you can use lot of option i tell you one of them.

- (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;
}

And whatever you want you can put in to these conditions.

or you can add tag with your textfield and check accordingly.Hide picker whenever you want or display picker and hide keyboard whenever you want.

酸甜透明夹心 2024-10-13 04:39:20
  @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
  @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];
    }

and the .h file

@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
情丝乱 2024-10-13 04:39:20

明白了,谢谢您的帮助

-(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 之前初始化

Got it THX for your help

-(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];
}

picker must be initialized before touching textField3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文