2012-02-08 5 views
0

alertview textfieldのテキスト入力をNSMutableArrayにコピーしようとしましたが、alertviewがポップアップしてテキスト入力フィールドに入力しましたが、OKを押すと警告表示が消えますがここに私の可変配列UialertviewテキストをNSMutableArray、ios 4.3

にdoesntのコピーテキストがここに私のコード

-(IBAction)add:(UIButton *)sender 
{ 
    addCustomStand = [[NSMutableArray alloc] init]; 
    UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter a Stand Location" 
                message:@" " 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil]; 

    UITextField *nameField = [[UITextField alloc] 
           initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)]; 
    [nameField setBackgroundColor:[UIColor whiteColor]]; 
    nameField.text = @""; 
    [dialog addSubview:nameField]; 

    if ([nameField text]){ 
     NSLog(@"Name Field %@ ",nameField.text); 
     [addCustomStand addObject:nameField.text]; 
    } 

    [nameField release]; 
    [dialog show]; 
    [dialog release]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 

    if([title isEqualToString:@"OK"]) 
    { 
     NSLog(@"Button 1 was selected."); 
     NSLog(@"StandLocations %@ ",addCustomStand); 
    } 
} 

である私の出力は、誰もが間違っていただきまし助けることができる

2012-02-07 20:26:57.315 Avicii[1399:b603] Name Field 
2012-02-07 20:26:59.720 Avicii[1399:b603] Button 1 was selected. 
2012-02-07 20:26:59.721 Avicii[1399:b603] StandLocations (
    "" 
) 

ログ画面上にありますそのコードで?

答えて

0

ユーザが入力した値を持っていない[nameField text]ためです:

は、代わりにあなたはこのような何かを行うことによって、あなたのclickedButtonAtIndex:方法の間に、あなたの配列に値をコピーする必要があります

UIAlertViewデリゲートメソッドで配列に追加するように変更してください。

-(IBAction)add:(UIButton *)sender 
{ 
    addCustomStand = [[NSMutableArray alloc] init]; 
    UIAlertView* dialog = [[UIAlertView alloc] initWithTitle:@"Enter a Stand Location" 
                message:@" " 
                delegate:self 
              cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"OK", nil]; 

    UITextField *nameField = [[UITextField alloc] 
           initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)]; 
    [nameField setBackgroundColor:[UIColor whiteColor]]; 
    nameField.text = @""; 
    // Note at this line 
    nameField.tag = 100; 
    // 
    [dialog addSubview:nameField]; 

    [nameField release]; 
    [dialog show]; 
    [dialog release]; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 

    if([title isEqualToString:@"OK"]) 
    { 
     // Note at this line 
     UITextField* nameField = (UITextField *)[alertView viewWithTag:100]; 
     [addCustomStand addObject:nameField.text]; 
     // 
     NSLog(@"Button 1 was selected."); 
     NSLog(@"StandLocations %@ ",addCustomStand); 
    } 
} 
+0

名前フィールタグ= 100;ラインはそこにいますか? –

+0

あなたのnameFieldにタグをつけているだけで、何でもかまいません。私は100を任意に選択します。あなたの名前フィールドには、サブビューに追加する前にタグ番号(100)を与えています。したがって、あなたのUIAlertViewデリゲートでは、 '[alertView viewWithTag:100]'を使ってnameFieldを取得できます。 –

0

警告ダイアログを表示する前に、nameField.textをaddCustomStand配列に追加しています。それを配列に追加するときには、値は空の文字列になります。あなたは[addCustomStand addObject:nameField.text];でそれを追加したときに、まだ

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 

    if([title isEqualToString:@"OK"]) 
    { 
     NSString *location; 
     UIView *view; 
     for (view in [alertView subviews]) { 

      if ([view isKindOfClass:[UITextField class]]) { 
       location = [(UITextField*)view text]; 
      } 
     } 

     if (location) { 
      [addCustomStand addObject:location]; 
     } 
    } 
}