开发者

UIPicker and Keyboard conflict

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 *)textFi开发者_高级运维eld{
        [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];
}


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.


  @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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜