【问题标题】:How to disable a index of a alertview in ios如何在 ios 中禁用警报视图的索引
【发布时间】:2016-06-01 21:42:06
【问题描述】:

我需要根据标志值禁用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 的按钮索引。谁能帮忙

【问题讨论】:

  • 设置 button.tag 并从标签获取视图并禁用它。
  • 如何为每个按钮索引创建标签。我已经为 alertview 创建了标签?
  • 您的 isCheck 值何时更改? @Arun
  • 它来自 webservice @ ronak
  • 所以,你可以做的是 [alert addButtonWithTitle:@"SMS"] 仅当 isCheck=1 @Arun

标签: ios objective-c ios7 uialertview


【解决方案1】:

您可以尝试如下代码:

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

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

【讨论】:

  • 如何处理 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
  • 太棒了 @ronak 我可以让 SMS 按钮可见,但用户在按下时不应执行任何操作。
  • 如果你不想点击 SMS 按钮上的任何内容,那么不要在 if([title isEqualToString:@"SMS"]) { NSLog(@"SMS"); 中处理任何内容}
  • 我在所有按钮索引中都有一个 Web 服务调用。
  • 在 Web 服务结果中我得到一个(isCheck=0 或 1)标志,如果 0 应该显示按钮索引并且用户不应该通过按下来执行任何任务。如果是 0 用户可以执行任务。
猜你喜欢
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多