2013-04-10 68 views
-2

我已成立了兩個名字警報視圖像這樣處理「確定」按鈕,從UIAlertView中

的UITextField * PLAYER1輸入; UITextField * player2;

UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Enter 2 player names" 
                message:@"\n\n\n" // IMPORTANT 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 

    player1 = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)]; 
    [player1 setBackgroundColor:[UIColor whiteColor]]; 
    [player1 setPlaceholder:@"player1"]; 
    [prompt addSubview:player1]; 

    player2 = [[UITextField alloc] initWithFrame:CGRectMake(12, 85, 260, 25)]; 
    [player2 setBackgroundColor:[UIColor whiteColor]]; 
    [player2 setPlaceholder:@"player2"]; 
    [prompt addSubview:player2]; 

    // set place 
    [prompt setTransform:CGAffineTransformMakeTranslation(0, 110)]; 
    [prompt show]; 
    //[prompt release]; 

    // set cursor and show keyboard 
    [player1 becomeFirstResponder]; 

現在我想處理「確定」按鈕單擊。我試圖做這樣的事情沒有運氣..

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
if (buttonIndex == 0) 
{ 
    NSLog(@"cancel"); 
} 
else 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OK works" message:@"no error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
} 
} 

從我讀過這應該工作。但是,當我按下「確定」按鈕時,沒有任何反應。

回答

5

您需要爲代表UIAlertView設置self

UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Enter 2 player names" 
                message:@"\n\n\n" // IMPORTANT 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
+0

嗯......我不能接受12分鐘的答案設置委託給自己太大。謝謝! – joshft91 2013-04-10 03:56:11

+0

從alertView方法中的兩個字段獲取文本的最佳方式是什麼? – joshft91 2013-04-10 04:09:35

+1

@ joshft91你可以分配標籤到文本框,然後從標籤獲取文本框和它的值。 – 2013-04-10 04:27:06

1

注:沒有關係原來的問題,但按照註釋「你能不能給我一些細節,以你的意思分配標籤文本框,然後得到的文本框&它的價值是什麼?」我在這裏張貼答案。

分配標籤,文本框

UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Enter 2 player names" 
                message:@"\n\n\n" // IMPORTANT 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 

    player1 = [[UITextField alloc] initWithFrame:CGRectMake(12, 50, 260, 25)]; 
    [player1 setBackgroundColor:[UIColor whiteColor]]; 
    [player1 setPlaceholder:@"player1"]; 
    [player1 setTag:100]; // added this 
    [prompt addSubview:player1]; 

    player2 = [[UITextField alloc] initWithFrame:CGRectMake(12, 85, 260, 25)]; 
    [player2 setBackgroundColor:[UIColor whiteColor]]; 
    [player2 setPlaceholder:@"player2"]; 
    [player2 setTag:200]; // added this 
    [prompt addSubview:player2]; 

    // set place 
    [prompt setTransform:CGAffineTransformMakeTranslation(0, 110)]; 
    [prompt show]; 
    //[prompt release]; 

    // set cursor and show keyboard 
    [player1 becomeFirstResponder]; 

檢索值

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
     if (buttonIndex == 0) 
     { 
      NSLog(@"cancel"); 
     } 
     else 
     { 
      UITextField *txtPlayer1 = (UITextField*)[alertView viewWithTag:100]; 
      NSLog(@"Value for player1: %@",txtPlayer1.text); 

      UITextField *txtPlayer2 = (UITextField*)[alertView viewWithTag:200]; 
      NSLog(@"Value for player2: %@",txtPlayer2.text); 

      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OK works" message:@"no error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
     } 
    } 
0

我用下面的代碼來檢查alertview的按鈕索引。

的陳述表示當UIAlertView中,然後...

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) { 
     NSLog(@"index 0 pressed ie cancel button"); 
     // do something when cancel pressed 
    } 
    else NSLog(@"index 1 pressed ie ok button"); 
     // do something when ok button pressed 
}