-2

我为两个要输入的名称设置了一个警报视图,如下所示

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

从我读过的内容来看,这应该可行。但是,当我按下“确定”按钮时,什么也没有发生。

4

3 回答 3

5

您需要selfUIAlertView.

UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Enter 2 player names"
                                                     message:@"\n\n\n" // IMPORTANT
                                                    delegate:self
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
于 2013-04-10T03:55:17.547 回答
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];
        }
    }
于 2013-04-10T05:28:17.277 回答
0

我使用以下代码检查警报视图的按钮索引。

如前所述,在显示 UIAlertview 时也将委托设置为 self,然后...

    -(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
}
于 2013-04-11T19:43:56.063 回答