2012-04-02 24 views
0

我有一個簡單的應用程序,將允許用戶標記的位置。應用程序1中的2個按鈕用於清除標記,1用於標記位置。用的UITextField添加用戶文字輸入地圖標註

我有工作UIAlertView中嵌入。我需要從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開發,只是不能讓這部分想出

回答

1

你需要捕捉文本字段中輸入時,駁回了警報。此代碼假定您保留新的或編輯的註釋的參考。

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

我不得不承認,我是一個完整的noob,並通過一些代碼,我已經從GitHub編輯去。我將如何保留新的或編輯過的註釋的參考? – matt 2012-04-02 18:03:49

+0

只需聲明該類的屬性。這是一個很平常的事... – Mundi 2012-04-02 23:32:07

+0

終於得到了它的工作,感謝您的幫助 – matt 2012-04-04 15:39:45

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’你可以傳遞給(空)變量標籤功能

這裏是modalView(自動被調用)的樣本,並將它運行「(無效)標籤」(沒有在它的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];