如何在ios中禁用alertview的索引

时间:2016-02-20 06:57:56

标签: ios objective-c ios7 uialertview

我需要根据标志值禁用alerview索引。

我的观点是:

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

的标志

如果isCheck = 1我必须禁用名为SMS的按钮索引。任何人都可以帮忙

1 个答案:

答案 0 :(得分:1)

您可以尝试的内容如下代码所示:

只有当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");
    }

}

OR

- (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");
        }
    }