0

I created a button in a "bottom bar." When the user presses the button, I am trying to show a UIAlertView, so the user can enter an address which will result in a blue pin being shown on the map. This pin should then be saved to NSUserDefaults, so that the location is saved each time the app is restarted.

Here is what I have so far. The user can enter the address in the UIAlertView, but nothing happens...

- (IBAction)selectHq:(UIBarButtonItem *)sender
{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Select Headquarters"
                                                    message:@"Enter Address"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Ok", nil];


    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];

    [alert show];

    UITextField *field = [alert textFieldAtIndex:0];
    field.placeholder = @"Enter HQ Address";


    if (!self.geocoder)
    {
        self.geocoder = [[CLGeocoder alloc] init];
    }

    NSString *hqAddress = [NSString stringWithFormat:@"%@", field.text];

    [self.geocoder geocodeAddressString:hqAddress completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([placemarks count] > 0)
        {
            CLPlacemark *placemark = [placemarks objectAtIndex:0];
            CLLocation *location = placemark.location;
            CLLocationCoordinate2D hqCoordinate = location.coordinate;

            NSLog (@"%f %f", hqCoordinate.latitude, hqCoordinate.longitude);




            MKCoordinateRegion region;
            MKCoordinateSpan span;
            span.latitudeDelta = 0.01;
            span.longitudeDelta = 0.01;
            region.span = span;
            region.center = hqCoordinate;

            MKPointAnnotation *hqAnnotation = [[MKPointAnnotation alloc] init];
            [hqAnnotation setCoordinate:hqCoordinate];
            [hqAnnotation setTitle:@"HQ"];
            [[self mapView] addAnnotation:hqAnnotation];

            [self.mapView setRegion:region animated:TRUE];
            [self.mapView regionThatFits:region];




        }
    }];


    [[NSUserDefaults standardUserDefaults] setObject:field.text forKey:HQ_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
Jack Philips
  • 57
  • 1
  • 10
George Friday
  • 585
  • 1
  • 6
  • 22

1 Answers1

0

You should implement the UIAlertViewDelegate and put your action code for alert button selection in to the delegate method.

- (IBAction)selectHq:(UIBarButtonItem *)sender
{
 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Select Headquarters"
                                                    message:@"Enter Address"
                                                   delegate:self
                                          cancelButtonTitle:@"Cancel"
                                          otherButtonTitles:@"Ok", nil];


    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [[alert textFieldAtIndex:0] setKeyboardType:UIKeyboardTypeNumbersAndPunctuation];

    [alert show];
    [alert release]  // if non-arc project.
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    // add your code here for tagging the map 
    // saving to nsuserdefaults.
  }
subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43