Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS alertview按钮上的操作_Ios_Xcode_Alertview - Fatal编程技术网

iOS alertview按钮上的操作

iOS alertview按钮上的操作,ios,xcode,alertview,Ios,Xcode,Alertview,我在菜单中有一个按钮,当触摸该按钮时,会弹出一条带有两个按钮的警告消息:“取消”和“是”。这是我的警报代码: UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exit game" message:@"Are you sure?" delega

我在菜单中有一个按钮,当触摸该按钮时,会弹出一条带有两个按钮的警告消息:“
取消
”和“
”。这是我的警报代码:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exit game"
                                                message:@"Are you sure?"
                                               delegate:nil
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Yes", nil];
[alert show];

是否可以将操作添加到按钮“
Yes
”?

您需要实现
UIAlertViewDelegate

并添加以下内容

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        // do stuff
    }
}

是的,很容易。看到你现在设置为nil的名为“delegate”的参数了吗?将其设置为一个对象。。。如果从视图控制器调用它,然后实现UIAlertViewDelegate的选择器,则通常为“self”

您还需要声明视图控制器符合UIAlertViewDelegate协议。在视图控制器的“private”continuation类中是一个很好的地方

@interface MyViewController() <UIAlertViewDelegate>
@end

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   NSLog(@"Button pushed: %d", buttonIndex);
}
@接口MyViewController()
@结束
-(无效)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引
{
NSLog(@“按钮按下:%d”,按钮索引);
}

在代码集中,UIAlertView委托:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exit game" message:@"Are you sure?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil]; [alert show];
将delegate设置为self后,在同一类中编写delegate函数,如下所示:

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

如果您有多个UIAlertView,您应该设置标记来描述调用哪个UIAlertView。