使用 UIAlertView 固定注解

Posted

技术标签:

【中文标题】使用 UIAlertView 固定注解【英文标题】:Pin Annotation with UIAlertView 【发布时间】:2013-09-08 21:58:09 【问题描述】:

我在“底栏”中创建了一个按钮。当用户按下按钮时,我试图显示一个 UIAlertView,因此用户可以输入一个地址,这将导致地图上显示一个蓝色图钉。然后应该将此 pin 保存到 NSUserDefaults,以便在每次重新启动应用程序时保存该位置。

这是我目前所拥有的。用户可以在 UIAlertView 中输入地址,但是没有任何反应……

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

【问题讨论】:

【参考方案1】:

您应该实现UIAlertViewDelegate 并将用于选择警报按钮的操作代码放入委托方法中。

- (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.
  

【讨论】:

它正在工作。针是紫色的。如何将图钉颜色更改为蓝色? @GeorgeFriday 看看***.com/questions/1185611/…。它可能会帮助你。

以上是关于使用 UIAlertView 固定注解的主要内容,如果未能解决你的问题,请参考以下文章

UIAlertView 和 UIAlertController

iOS5:有人设法用三个按钮和文本字段(UIAlertViewStylePlainTextInput)修复 UIAlertView 吗?

IOS UIAlertController 使用方法

IOS AlterView的使用(IOS8.0以前使用)

Cocoa Touch - 处理被打断或丢失的触摸

我可以移动 UIAlertView 吗?