如何在 ios 中禁用警报视图的索引

How to disable a index of a alertview in ios

我需要根据标志值禁用警报视图索引。

我的看法是:

UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Reminder" message:@"Send a reminder through" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[alert addButtonWithTitle:@"Email"];
[alert addButtonWithTitle:@"SMS"];
[alert addButtonWithTitle:@"TS"];
[alert addButtonWithTitle:@"Cancel"];
[alert show];

我有一个名为 isCheck=0 或 1 的标志

if isCheck=1 我必须禁用名为 SMS 的按钮索引。谁能帮忙

您可以尝试如下代码:

仅当 isCheck 为 1 时才会添加短信按钮。

UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Reminder" message:@"Send a reminder through" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alert addButtonWithTitle:@"Email"];

    if (isCheck==1) {
        [alert addButtonWithTitle:@"SMS"];
    }
    [alert addButtonWithTitle:@"TS"];
    [alert addButtonWithTitle:@"Cancel"];

    [alert show];

将以下委托方法添加到您的代码中,以处理单击 UIAlertView 的特定按钮时发生的任何事情:

- (void)alertView:(UIAlertView *)alert didDismissWithButtonIndex:(NSInteger)buttonIndex {

NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

    if([title isEqualToString:@"Email"])
    {
        NSLog(@"Email");
    }
    if([title isEqualToString:@"SMS"])
    {
        NSLog(@"SMS");
    }
    if([title isEqualToString:@"TS"])
    {
        NSLog(@"TS");
    }
    if([title isEqualToString:@"Cancel"])
    {
        NSLog(@"Cancel");
    }

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

        if([title isEqualToString:@"Email"])
        {
            //Email button Clicked: Do whatever you want
            NSLog(@"Email");
        }
        if([title isEqualToString:@"SMS"])
        {
            //SMS button Clicked: Do whatever you want
            NSLog(@"SMS");

            //Call your Web Service
            if(isCheck==1){
              //Do something
            }
            else{
             //Do Nothing
            }

        }
        if([title isEqualToString:@"TS"])
        {
            //TS button Clicked: Do whatever you want
            NSLog(@"TS");
        }
        if([title isEqualToString:@"Cancel"])
        {
            //Cancel button Clicked: Do whatever you want
            NSLog(@"Cancel");
        }
    }