2016-07-16 6 views
0

In meiner App habe ich zwei textFields, durch Klicken auf jede textField ich kann Orte laden von Google Orte Autocomplete.In Benutzerhandler ich habe zuweisen, in welchem ​​Textfeld ich das Ergebnis zeigen.Wenn ich klicken Sie auf erstes Textfeld, Der Wert zeigt das zweite Textfeld an. Ich setze Tag-Werte für beide Textfelder. Unten ist mein voller Code. Danke im Voraus.Wie verwende ich Autocomplete mit zwei Textfeldern ios?

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

} 

Antwort

0

Sie benötigen eine weitere Instanz textField in Ihrem viewController wie diese

UITextField *selTextField; 

nun die Referenz geklickt Textfield auf selTextField gesetzt zu erklären, in der beide IBAction wie diese

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

Jetzt in Delegiertenmethode von 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

Nein, deklariere kein globales. Deklarieren Sie eine Instanzvariable. Es gibt einen großen Unterschied. – rmaddy

+0

@rmaddy Bearbeitete Antwort für die Variable instance. –

+0

Es funktioniert für mich.Thanks @Nirav – Vignesh

Verwandte Themen