将用户文本输入添加到Map Annotation

时间:2012-04-02 16:55:39

标签: iphone objective-c ios mapkit

我有一个简单的应用程序,允许用户标记位置。应用程序1中的2个按钮用于清除标签,1个用于标记位置。

我在UITertfield中嵌入了UITextfield。我需要从UITextfield获取用户输入并将其应用于地图上的注释。我的标签按钮代码位于

之下
- (void) tag
{
MapAnnotation *annotation = [[MapAnnotation alloc] initWithCoordinate:current.coordinate];

NSString *locString = [NSString stringWithFormat:@"%f, %f", current.coordinate.latitude, current.coordinate.longitude];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeStyle = NSDateFormatterLongStyle;
//insert UITextfield
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Name your Location" message:nil delegate:self cancelButtonTitle:@"Continue" otherButtonTitles: nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.keyboardType = UIKeyboardTypeDefault;
alertTextField.placeholder = @"Enter title of your tag";

[alert  show];
// I have tried numerous variations of NSString below
//NSString *pintitle = NSLog(@"%@", alertTextField.text);
annotation.title = pintitle; 
annotation.subtitle = locString;

[mapView addAnnotation:annotation];
}

我尝试了几个选项,有些返回空值而其他没有。我正在开始iOS开发,只是无法弄清楚这部分

2 个答案:

答案 0 :(得分:1)

当警报被取消时,您需要捕获文本字段的输入。此代码假定您保留对新注释或已编辑注释的引用。

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
   UITextField *textField = [alertView textFieldAtIndex:0];
   self.annotationBeingEdited.subtitle = textField.text;
}

答案 1 :(得分:0)

尝试从标签按钮中删除AlertView,然后在用户按下按钮时使用modalView(见下文)。您需要编辑AlertView以添加“OtherButtonTitles”和“.tag =”,例如:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Name your Location"
    message:@"Enter Location"
    delegate:self 
    cancelButtonTitle:@"Cancel"
    otherButtonTitles:@"Continue", nil];
alertView.tag = 800;

我建议将“pinTitle”变成一个可以传递给(void)标记函数的变量

这是modalView的一个示例(自动被调用),让它运行你的“(void)标签”(不包含alertview),看看是否有效。

#pragma mark alert box delegate methods and actions
- (void)modalView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{
    // the second button (1) was pressed
    if (buttonIndex == 1 && alertView.tag == 800) {
        //NSLog(@"modalView dismiss button %i", buttonIndex);
        // Now run the geolocation (void)tag
        [self tag];
    }
}

另外,请确保同时发布注释,例如:

MapAnnotation *annotation = [[[MapAnnotation alloc] initWithCoordinate:current.coordinate] autorelease];