2010-12-03 92 views
1

我有什麼是3 textFields和每當我觸摸textField1或textField2選取器更改值,並沒有鍵盤。當我觸摸textField3鍵盤出現,選取器隱藏。現在,如果我通過單擊返回,然後單擊textField1選擇器退出鍵盤,但如果我不不關閉鍵盤按鈕保持在選擇器。當鍵盤firstResponder我需要的是(我看到它在屏幕上)來隱藏它,如果我在textField1的點擊,只看到拾取UIPicker和鍵盤衝突

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

回答

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

無論你想要什麼,你可以把這些條件。

或者您可以添加標籤與您的文本字段並相應地檢查。任何時候隱藏選擇器或顯示選擇器和隱藏鍵盤,只要你想。

+0

羅伯特這不工作,要麼你被初始化'重新錯了。親愛的 – Spire 2010-12-03 11:22:45

+0

@spire這絕對的作品,但它對你如何使用這根據you.and這就是答案編輯由Ishu – Ishu 2010-12-03 11:43:13

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 
0

明白了THX的幫助

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

選擇器必須觸碰文字欄前