如何根据uitextfield的标签ID获取uitextfield中的值。

时间:2016-05-03 06:15:59

标签: ios uitextfield

我正与GMSAutocompleteViewController合作,我的UITextfield中有三个uiviewcontroller。当第一个UItextfield点击时,我移动到带有标签号的GMSAutocompleteViewController,但现在我想在文本字段中显示地名,用户在gmsautocompleteviewcontroller方法中点击该地名。请看我的代码。

    - (void)textFieldDidBeginEditing:(UITextField *)textField
{


 NSLog(@"start editing");

    if (textField.tag ==1) {
        NSLog(@"first");
        GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
        acController.delegate = self;
        [self presentViewController:acController animated:YES completion:nil];


       }
    else if (textField.tag ==2)
    {
        NSLog(@"second");
        GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
        acController.delegate = self;
        [self presentViewController:acController animated:YES completion:nil];
    }
    else if (textField.tag ==3)
    {
        NSLog(@"third");
        GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
        acController.delegate = self;
        [self presentViewController:acController animated:YES completion:nil];
       }




      }


    - (void)viewController:(GMSAutocompleteViewController *)viewController
      didAutocompleteWithPlace:(GMSPlace *)place {
    // Do something with the selected place.
    NSLog(@"Place name %@", place.name);
    NSLog(@"Place address %@", place.formattedAddress);
    NSLog(@"Place attributions %@", place.attributions.string);
    NSLog(@"lat and log%f", place.coordinate.latitude);
    NSLog(@"lang %f", place.coordinate.longitude);



    value = place.name;


    [self dismissViewControllerAnimated:YES completion:nil];
       }

    - (void)viewController:(GMSAutocompleteViewController *)viewController
    didFailAutocompleteWithError:(NSError *)error {
    // TODO: handle the error.
    NSLog(@"error: %ld", (long)[error code]);
    [self dismissViewControllerAnimated:YES completion:nil];
        }

     // User canceled the operation.
     - (void)wasCancelled:(GMSAutocompleteViewController *)viewController {
    NSLog(@"Autocomplete was cancelled.");
    [self dismissViewControllerAnimated:YES completion:nil];
      }

1 个答案:

答案 0 :(得分:2)

全局声明文本字段

 UITextField *tappedTextField;

检查以下代码

- (void)textFieldDidBeginEditing:(UITextField *)textField{
tappedTextField = textField;
}
 //gmsautocomplete delegate
 - (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithPlace:(GMSPlace *)place {
// Do something with the selected place.
NSLog(@"Place name %@", place.name);
NSLog(@"Place address %@", place.formattedAddress);
NSLog(@"Place attributions %@", place.attributions.string);
NSLog(@"lat and log%f", place.coordinate.latitude);
NSLog(@"lang %f", place.coordinate.longitude);



tappedTextField.text = place.name;


[self dismissViewControllerAnimated:YES completion:nil];
}