首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertView IndexButton

UIAlertView IndexButton
EN

Stack Overflow用户
提问于 2013-10-27 14:07:37
回答 5查看 197关注 0票数 0

我的地图上有注释的代码.

代码语言:javascript
复制
//alert view

if ([ann.title isEqual: @"Al-saidiya"]) {

    NSString *msg=@"Phone No : 079011111";
    UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Contact" message:msg delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"Call Us", nil];



    [alert1 show];
}
else if ([ann.title isEqual: @"Al-Kadmiya"]) {


    NSString *msg=@"Phone No : 07902222222";
    UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:@"Contact" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"Call Us", nil];
    [alert2 show];
}

else if ([ann.title isEqual: @"Palestine St"]) {

    NSString *msg=@"Phone No : 0790333333";
    UIAlertView *alert3 = [[UIAlertView alloc]initWithTitle:@"Contact" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: @"Call Us",nil];
    [alert3 show];
}

else if ([ann.title isEqual: @"Karada Maryam"]){

    NSString *msg=@"Phone No : 07905867";
    UIAlertView *alert4 = [[UIAlertView alloc]initWithTitle:@"Contact" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"Call Us", nil];
    [alert4 show];
}

else if ([ann.title isEqual: @"Mansour Office"])  {

   NSString *msg=@"Phone No : 07954212";
    UIAlertView *alert5 = [[UIAlertView alloc]initWithTitle:@"Contact" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: @"Call Us",nil];
    [alert5 show];
}

else if ([ann.title isEqual: @"Hunting Club"]) {


    NSString *msg=@"Phone No : 079337745";
    UIAlertView *alert6 = [[UIAlertView alloc]initWithTitle:@"Contact" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: @"Call Us",nil];
    [alert6 show];
}
else if ([ann.title isEqual: @"Al-jadriya"])  {

    NSString *msg=@"Phone No : 07976231";
    UIAlertView *alert7 = [[UIAlertView alloc]initWithTitle:@"Contact" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: @"Call Us",nil];
    [alert7 show];
}

else if ([ann.title isEqual: @"Al-jamea'a"]) {

    NSString *msg=@"Phone No : 07865323";
    UIAlertView *alert8 = [[UIAlertView alloc]initWithTitle:@"Contact" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: @"Call Us",nil];
    [alert8 show];
}

}

当我应用这个方法时:

代码语言:javascript
复制
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex==1){
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://576576576"]]];
        NSLog(@"It works!");
    }
}

它已经应用于上面的每一个警报对象,并以相同的号码。我希望每个警报对象都有自己的电话号码,当我想打电话时。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-10-27 15:05:56

首先在上面的代码中设置警告视图中的标记,然后在下面的方法中设置标记。就像这样:-

代码语言:javascript
复制
     -(void)alertView:(UIAlertView *)alertView    
   clickedButtonAtIndex:(NSInteger)buttonIndex 
 {

    int indexValue=alertView.tag;

   switch (indexValue)
  {
  case 0:
    NSLog (@"zero");
   //your code
    break;
  case 1:
    NSLog (@"one");
  //your code
    break;
  case 2:
    NSLog (@"two");
  //your code
    break;
  case 3:
    NSLog (@"three");
  // your code
    break;
  case 4:
    NSLog (@"four");
  //your code
    break;
  case 5:
    NSLog (@"five");
  // your code
    break;
...... Up to

   case 8:
  // your code
   break;
  default:
    NSLog (@"done");
    break;
   }
票数 0
EN

Stack Overflow用户

发布于 2013-10-27 14:12:36

只需在警告视图中添加一个标记即可。

代码语言:javascript
复制
if ([ann.title isEqual: @"Al-saidiya"]) {

    NSString *msg=@"Phone No : 079011111";
    UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"Contact" message:msg delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"Call Us", nil];

    alert1.tag = 0; // <--

    [alert1 show];
}

并检查alertView:clickedButtonAtIndex:中的标记

代码语言:javascript
复制
if (alertView.tag == 0) {
  // call Al-saidiya
}
...
票数 2
EN

Stack Overflow用户

发布于 2013-10-27 14:36:07

好吧,即使tilo提出的解决方案有效,当您有多个对象实例(如UIAlertview )时,我认为这并不是正确的方法。

我建议你用积木代替。这些类别 (项目对UIActionSheet使用相同的模式)允许您将操作块绑定到alertView中的特定按钮。

使用这种方法,您可以使用委托模式消除所有if/switch语句。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19618952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档