UIAlertView IndexButton

一个人想着一个人 提交于 2019-12-13 03:34:01

问题


I have this code right here for annotations in my map...

//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];
}

}

And when i apply this method ::

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

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

it has been applied on every alert objects above there and took the same number.i want every alert object to get its own phone number when i want to call.


回答1:


First set the tag in your alertview in above code then in your below method. Try like this:-

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



回答2:


Just add a tag to your alert views

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];
}

and check the tag in alertView:clickedButtonAtIndex::

if (alertView.tag == 0) {
  // call Al-saidiya
}
...



回答3:


Well even if the solution proposed by tilo works, I think is not the right approach when you have multiple instances of objects like UIAlertview.

I would like to suggest you to use blocks instead. These categories (the project use the same pattern for UIActionSheet) allow you to bind an action block to a specific button in your alertView.

Using this approach you can get rid of all the if/switch statements using the delegate pattern.




回答4:


As the title and the phone number is a 1:1 relationship I'd use a dictionary:

NSDictionary *titlesAndMessages = @{@"Al-saidiya" : @"Phone No : 079011111",
                                    @"Al-Kadmiya" : @"Phone No : 07902222222",
                                    @"Palestine St" : @"Phone No : 0790333333"};

...

NSString *messageString = nil;
for (NSString *keyTitle in [titlesAndMessages allKeys]) {
    if ([ann.title isEqualToString:keyTitle]) {
        messageString = [titlesAndMessages objectForKey:keyTitle];
        break;
    }
}

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Contact" message:messageString delegate:self cancelButtonTitle:@"ok" otherButtonTitles:@"Call Us", nil];
[alert show];

}

This scales a lot better as you won't have to write any additional code to expand, just add entries to the dictionary (automagically or otherwise).




回答5:


Using UIAlertViewDelegate is really clumsy. I recommend everyone use PSAlertView for any non-trivial use of alerts.

Using this, the code becomes simple and self contained.

- (void)promptToContact:(NSString *)message 
             withNumber:(NSString *)phoneNumber
{
    PSAlertView *alert = [[PSAlertView alloc] initWithTitle:@"Contact"];
    [alert setCancelButtonWithTitle:@"Dismiss" block:^{}];
    [alert addButtonWithTitle:@"Call" block:^{
        NSString *urlString = [NSString stringWithFormat:@"telprompt://%@", phoneNumber];
        NSURL *url = [NSURL urlWithString:urlString];
        [[UIApplication sharedApplication] openURL:url];
     }];
    [alert show];
}


来源:https://stackoverflow.com/questions/19618952/uialertview-indexbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!