2013-10-27 29 views
0

我有这样的代码就在这里在我的地图标注......UIAlertView中Index键

//alert view 

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

    NSString *[email protected]"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 *[email protected]"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 *[email protected]"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 *[email protected]"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 *[email protected]"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 *[email protected]"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 *[email protected]"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 *[email protected]"Phone No : 07865323"; 
    UIAlertView *alert8 = [[UIAlertView alloc]initWithTitle:@"Contact" message:msg delegate:self cancelButtonTitle:@"ok" otherButtonTitles: @"Call Us",nil]; 
    [alert8 show]; 
} 

}

当我应用此方法::

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

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

它已经被应用在每个警报对象上面,并采取相同的号码。我希望每个警报对象获得自己的电话号码,当我想打电话。

+0

此问题与Xcode无关。 – 2013-10-27 14:11:02

+0

问题是什么? – RyanR

回答

0

首先在上面的代码中的alertview中设置标签,然后在下面的方法中设置标签。试试这样: -

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

谢谢你这个人工作得很好..感谢很多 –

+0

你最欢迎:) –

2

只是一个标签添加到您的警报意见

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

    NSString *[email protected]"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:标签:

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

那么即使通过TILO作品提出的解决方案,我认为是不正确的方法当你有像UIAlertview这样的对象的多个实例时。

我想建议你使用块代替。 These categories(该项目对UIActionSheet使用相同的模式)允许您将动作块绑定到alertView中的特定按钮。

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

1

正如标题和电话号码是1:1的关系,我会使用一个字典:

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

}

这可以更好地扩展,因为您不必编写任何额外的代码来扩展,只需将条目添加到字典(自动o否则)。

1

使用UIAlertViewDelegate确实很笨拙。我建议每个人都使用PSAlertView进行任何不重要的警报使用。

使用此代码变得简单和自包含。

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