按钮上的iOS alertview操作

Stu*_*mpp 2 xcode ios alertview

我在菜单中有一个按钮,当触摸时,会弹出一个警告消息,其中包含两个按钮:" Cancel"和" Yes".这是我对警报的代码:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exit game"
                                                message:@"Are you sure?"
                                               delegate:nil
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Yes", nil];
[alert show];
Run Code Online (Sandbox Code Playgroud)

是否可以在按钮" Yes"中添加动作?

Sau*_*kla 11

在您的代码中设置UIAlertView委托:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exit game" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil]; [alert show];
Run Code Online (Sandbox Code Playgroud)

正如您已将委托设置为self,请在同一个类中编写委托函数,如下所示:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) { // Set buttonIndex == 0 to handel "Ok"/"Yes" button response
    // Cancel button response
    }}
Run Code Online (Sandbox Code Playgroud)