UIAlertView 按钮动作代码

3

请问有人知道如何为 UIAlertview 中的按钮创建动作吗?如果知道,请指导我。

5个回答

8
- (void)alertView:(UIAlertView *)alertView 
         didDismissWithButtonIndex:(NSInteger) buttonIndex 
{
    if (buttonIndex == 0)
    {
        NSLog(@"Cancel Tapped.");
    }
    else if (buttonIndex == 1) 
    {    
        NSLog(@"OK Tapped. Hello World!");
    }
}

尝试这段代码,它会对你有用...


4

当在UIAlertView中点击按钮时,它的代理方法

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

被调用。您的代理必须实现此方法并检查按下了哪个按钮。

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    switch (buttonIndex) {
        case 0:
            // Do something for button #1
            break;
        case 1:
            // Do something for button #2
            break;
        ...
    }
}

如果您有多个警报视图,可以通过它们的标题进行区分,如下所示:
if ([alertView.title isEqualToString: yourAlertView.title]) {
    // proceed...
}

@Malloc 使用 %i 按钮索引是整数而不是双精度。 - Albert Renshaw

2

2
请使用以下代码:

首先为UIAlertView设置代理,然后编写其代理方法如下...

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
 {     
     if (buttonIndex == 0) {
        //Some Implementation
     } else if(buttonIndex == 1) {
        //Some Implementation
     }
 }

1
如果您想要为 UIAlerView 按钮获取操作,
您需要使用 UIAlertViewDelegate 和其方法以获取操作。
参考资料,

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接