2012-07-23 2 views
8

하나의보기에 8 개의 텍스트 필드가 있습니다. pickerview를 사용하여 각각을 채우고 싶습니다. 다른 텍스트 필드에 대해 서로 다른 배열의 항목 목록을 표시하는 pickerview를 1 개 가질 수 있습니까?하나의보기에서 여러 텍스트 필드에 대해 하나의 UIPickerView를 사용하는 방법은 무엇입니까?

샘플 코드를 사용하여 프로그래밍 방식으로 어떻게 설명 할 수 있습니까?

또한 뷰가로드 될 때 선택기 뷰를 숨기고 해당 데이터 목록이있는 텍스트 필드를 클릭 할 때만 표시해야합니까? 데이터를 선택하고 해당 데이터 목록이있는 다른 텍스트 필드를 클릭하면 사라집니다.

xcode를 처음 사용했습니다. 어떤 도움을 많이 주시면 감사하겠습니다. 고맙습니다.

답변

11

일부 조건에 따라 다른 데이터를 표시하는 PickerView는 하나만 사용할 수 있습니다.

내 아이디어는 다음과 같습니다. 8 개의 다른 배열을 만들 수 있습니다. 하나는 UITextField가 탭 된 위치에 따라 UIPickerView를 채우는 것입니다. 있는 viewDidLoad에있는 NSArray 같은 인터페이스를 선언하고 초기화 :

array1st = ... 
array2nd = ... 
array3d = ... 
//and until 8. 

그런 다음 당신은 그냥 (NSArray *currentArray을 같은) 작성해야 그 배열을 가리 키도록하고, 심지어 그것이 될 것입니다 = 초기화하기 위해서 필요하지 않은 다른를 만들 올바른 배열을 가리키는 데만 사용됩니다.

그래서 당신은 당신의 뷰 컨트롤러에 UITextFields의 대리자를 설정해야하고, 방법 당신은 UITextField에 누구인지 확인 currentArray으로 올바른 배열을 할당 reloadData으로 UIPickerView를 다시로드, 같은에서 NO 반환 textFieldShouldBeginEditingtextFieldShouldBeginEditing 방법. currentTextField는 "편집 된"currentUITextField가 무엇인지 알기위한 포인터입니다. 선택 도구에서 데이터를 선택한 후 텍스트를 설정할 수 있도록이 필드를 저장해야합니다. 인터페이스에서 currentTextField를 선언하십시오.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    currentTextField = textField; 
    if (textField == myFirstTextView) 
    { 
     currentArray = array1st; 
     [pickerView reloadData]; 
     [self animatePickerViewIn]; 
     return NO; 
    } 
    //and this way until 8th 
} 

그래서, 뷰 컨트롤러, 사실 즉, 현재 배열에 따라 UIPickerView를 채우고 UIPickerViewDelegate 때 당신은 단지 데이터를 얻을 수있는 배열로 currentArray를 사용, 아무것도 변경할 필요가 없습니다 에서. 이제

의 pickerView 보여 :

함께 IBOutlet을 연결 한 후를, 당신의 viewDidLoad에서 두 가지 작업을 수행해야 다음 UIPickerView의 프레임을 설정하고 하위 뷰로 추가 : 이제

pickerView.frame = CGRectMake(0,self.view.frame.size.height, pickerView.frame.size.width, pickerView.frame.size.height); 
pickerView.delegate = self; 
pickerView.dataSource = self; 
//or you can do it via Interface Builder, right click the pickerView and then connect delegate and dataSource to the file's owner 
[self.view addSubview:pickerView]; 

사용자가 UITextField를 두 드릴 때 호출 한 animatePickerViewIn이라는 메서드를 만들어야합니다.

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 

    return [currentArray count]; 
} 


-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 1; 
} 

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    return [currentArray objectAtIndex:row]; 
} 

을 그리고 사용자가 pickerView에서 행을 선택하면, 당신은 대리자 메서드로 나타납니다

-(void)animatePickerViewIn 
{ 

    [UIView animateWithDuration:0.25 animations:^{ 
     [pickerView setFrame:CGRectMake(0, pickerView.frame.origin.y-pickerView.frame.size.height, pickerView.frame.size.width, pickerView.frame.size.height)]; 
    }]; 
} 

은 pickerView의 데이터를로드합니다.

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    //and here you can do in two ways: 
    //1 
    [currentTextField setText:[currentArray objectAtIndex:row]]; 
    //2 
    [currentTextField setText:[self pickerView:pickerView titleForRow:row inComponent:component]]; 
} 
0

1. 도구 막대를 완료하고 barButtonItem을 사용하여 pickerView를 숨겨야합니다.

2. 이제 textFields tagValues에 다음을 입력하십시오. textFiled1.tag=100; 그런 다음이 태그 값을 사용하여 pickerView 배열을 채 웁니다. 사용자가 위임 방법, pickerView에서 어떤 가치를 선택 3.When

작성

if(textField.tag== @"Your Tag Value") 
{ 
     textField.text=[pickerArray objectAtIndex:row]; 
} 

나는 당신이 지금 당신이 solution.Good 행운의 요점을 가지고 코드를 직접 시도 할 것 같아요!

1

사용이 : 그래서 그냥 값이 선택으로 currentTextField의 텍스트를 설정합니다.

- (void)textFieldDidBeginEditing:(UITextField *)textField{ 
    MyPickervie.delegate=self; 
} 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)myLocationPickerView; 
{ 
    return 1; 
} 

-(void)myLocationPickerView:(UIPickerView *)myLocationPickerView didSelectRow:   (NSInteger)row inComponent:(NSInteger)component; 
{ 

    switch(textfield.tag){ 
    Case 0: 
      label.text=[arraNo objectAtindex:row]; 
      break; 
    default: break; 
    } 
} 
-(NSInteger)myLocationPickerView:(UIPickerView *)myLocationPickerView numberOfRowsInComponent:(NSInteger)component; 
{ 

switch(textfield.tag){ 
    Case 0: 
      return [arrayNo count]; 
      break; 
    default: break; 
    } 
} 

-(NSString *)myLocationPickerView:(UIPickerView *)myLocationPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component; 
{ 
    switch(textfield.tag){ 
    Case 0: 
      return [arrayNo objectAtIndex:row]; 
      break; 
    default: break; 
    } 

} 

스위치 조건에서 8 가지 케이스를 사용하여 다른 배열을 호출하고 pickerView에 설정해야하는 경우.

+0

안녕하세요, textfield.tag이 무엇인지 설명해 주시겠습니까? 그것은 스위치 필드에서 언명 된 텍스트 필드를 말하는가? 및 myLocationPickerView 어떤 label.text가 텍스트를 설정합니까? –

+0

textfield.tag 여기서 .h 파일로 선언 한 텍스트 필드를 사용하고 IBOutlate 할 수 있습니다. 또한 picker 뷰에서 선택된 값은 label.text를 사용하여 레이블에 표시됩니다. 선택한 값을 표시하려는 위치 또는 레이블을 사용할 수 있습니다. –

1

이 코드는 3 개의 텍스트 필드로 테스트했습니다.

그리고 문제를 완벽하게 해결했습니다.

1) 글로벌 UITextField 하나를 가져옵니다.

2) 텍스트 필드 편집의 위임 방법에서 전역 텍스트 필드를 현재 텍스트 필드로 설정합니다.

3) 피커 값 변경 방법에서 전역 텍스트 필드의 텍스트를 설정하면 선택한 텍스트 필드의 값이 변경됩니다.

아래 코드가 올바르게 작동하고 있습니다.

코딩 즐기기.

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView 
{ 
return 1; 
} 

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component 
{ 
return 10; 
} 

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
return [NSString stringWithFormat:@"Test %d",row + 1]; 

} 

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 

test.text = @"XYZ"; 

} 

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
test = textField; 

return YES; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{  
[textField resignFirstResponder]; 

return YES; 
} 
+2

안녕하십니까, 죄송합니다. 지침에 따라 어려움을 겪고 있습니다. 당신은 당신의 2, 3 점을 정교하게 설명 할 수 있습니까? –

+1

- (BOOL) textFieldShouldBeginEditing : (UITextField *) textField 및 - (BOOL) textFieldShouldReturn : (UITextField *) textField는 UITextField의 대리자 메서드입니다. 키보드를 표시 할 때 키보드 기능을 관리합니다. 이 경우 키보드와 현재 선택기를 숨길 수 있습니다. – Hemang

+2

그리고 picker에서 처음 4 개의 메소드는 picker의 모든 기본 기능을 관리하는 delegate 메소드입니다. 즉, 피커의 행 수, 피커의 데이터, 피커 값이 변경 될 때 수행 할 작업 및 기타 사항 .... xib 및 view 컨트롤러의 .h 파일에서 델리게이트를 설정해야합니다. – Hemang

관련 문제