处理UIAlertView中的"确定"按钮

jos*_*t91 -2 objective-c

我有一个警报视图设置为两个名称输入,如此

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];
Run Code Online (Sandbox Code Playgroud)

现在我想处理"确定"按钮点击.我试图做这样的事情没有运气..

- (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];
}
}
Run Code Online (Sandbox Code Playgroud)

从我所读到的,这应该工作.但是当我按下"确定"按钮时没有任何反应.

gra*_*ver 5

您需要self为代理设置UIAlertView.

UIAlertView *prompt = [[UIAlertView alloc] initWithTitle:@"Enter 2 player names"
                                                     message:@"\n\n\n" // IMPORTANT
                                                    delegate:self
                                           cancelButtonTitle:@"OK"
                                           otherButtonTitles:nil];
Run Code Online (Sandbox Code Playgroud)