2016-07-16 2 views
0

내 응용 프로그램에서 두 textFields, 각 textField 클릭하여 수 있습니다. Google 장소 autocomplete.In 사용자 처리기에서로드 할 장소를 선택할 수 있습니다. 텍스트 필드에서 내가 result.If 표시해야 할 첫 번째 텍스트 필드를 할당 할 할당 할 수 있습니다. 두 번째 텍스트 필드에 값을 설정합니다. 두 텍스트 필드 모두에 태그 값을 설정합니다. 아래는 내 전체 코드입니다. 미리 감사드립니다.두 텍스트 필드에 자동 완성을 사용하는 방법은 무엇입니까?

//when first textfield clicked 
- (IBAction)onLaunchClicked:(id)sender { 
     GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init]; 
     acController.delegate = self; 
     [self presentViewController:acController animated:YES completion:nil]; 

    } 

//when second textfield clicked 
- (IBAction)to_click:(id)sender { 
     GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init]; 
     acController.delegate = self; 
     [self presentViewController:acController animated:YES completion:nil]; 
    } 


// Handle the user's selection. 
    - (void)viewController:(GMSAutocompleteViewController *)viewController 
    didAutocompleteWithPlace:(GMSPlace *)place { 
     [self dismissViewControllerAnimated:YES completion:nil]; 
     // Do something with the selected place. 
     NSLog(@"Place name %@", place.name); 
     NSLog(@"Place address %@", place.formattedAddress); 
     NSLog(@"Place attributions %@", place.attributions.string); 

//have to set values in correct textfields 
    if (textfield.tag == 10001){ 
    from_txt.text=place.formattedAddress; 
    } 
    else { 
    to_txt.text= place.formattedAddress; 
     } 

} 

답변

0

당신은 지금 selTextField에 클릭에 textField의 기준 설정이

UITextField *selTextField; 

처럼 viewController에 하나 더 예를 textField을 선언 할 필요가 당신의 지금이

- (IBAction)onLaunchClicked:(id)sender { 
    self.selTextField = (UITextField*) sender; 
    GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init]; 
    acController.delegate = self; 
    [self presentViewController:acController animated:YES completion:nil]; 
} 

- (IBAction)to_click:(id)sender { 
    self.selTextField = (UITextField*) sender; 
    GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init]; 
    acController.delegate = self; 
    [self presentViewController:acController animated:YES completion:nil]; 
} 

같은 두 IBAction 위임 방법에서 GMSAutocompleteViewController

- (void)viewController:(GMSAutocompleteViewController *)viewController 
didAutocompleteWithPlace:(GMSPlace *)place { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    // Do something with the selected place. 
    NSLog(@"Place name %@", place.name); 
    NSLog(@"Place address %@", place.formattedAddress); 
    NSLog(@"Place attributions %@", place.attributions.string); 

    //Set textField value 
    self.selTextField.text=place.formattedAddress; 
} 
+1

아니요, 전체를 선언하지 마십시오. 인스턴스 변수를 선언하십시오. 큰 차이가 있습니다. – rmaddy

+0

@rmaddy 인스턴스 변수에 대한 수정 된 답변입니다. –

+0

그게 나를 위해 일해. 고마워요 @Nirav – Vignesh