Ios UIAlertView索引按钮
我的地图上有注释的代码Ios UIAlertView索引按钮,ios,objective-c,uialertview,Ios,Objective C,Uialertview,我的地图上有注释的代码 //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" otherButtonTi
//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];
}
}
当我采用这种方法时:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex==1){
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://576576576"]]];
NSLog(@"It works!");
}
}
它已应用于上面的每个警报对象,并使用相同的号码。我希望每个警报对象在我想打电话时都有自己的电话号码。只需在警报视图中添加一个标记即可
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];
}
只需在警报视图中添加一个标记
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];
}
即使tilo提出的解决方案有效,我认为当您有多个对象实例(如UIAlertview)时,这不是正确的方法 我建议你改用积木。 (项目使用与UIActionSheet相同的模式)允许您将操作块绑定到alertView中的特定按钮
使用此方法,您可以使用委托模式摆脱所有if/switch语句。即使tilo提出的解决方案有效,我认为当您有多个对象实例(如UIAlertview)时,这不是正确的方法 我建议你改用积木。 (项目使用与UIActionSheet相同的模式)允许您将操作块绑定到alertView中的特定按钮
使用此方法,您可以使用委托模式删除所有if/switch语句。首先在上述代码中的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;
}
首先在上面的代码中的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;
}
由于标题和电话号码是1:1的关系,我会使用字典:
NSDictionary *titlesAndMessages = @{@"Al-saidiya" : @"Phone No : 079011111",
@"Al-Kadmiya" : @"Phone No : 07902222222",
@"Palestine St" : @"Phone No : 0790333333"};
}
这样可以更好地扩展,因为您不必编写任何额外的代码来扩展,只需将条目添加到字典(自动或其他方式)。由于标题和电话号码是1:1的关系,我会使用字典:
NSDictionary *titlesAndMessages = @{@"Al-saidiya" : @"Phone No : 079011111",
@"Al-Kadmiya" : @"Phone No : 07902222222",
@"Palestine St" : @"Phone No : 0790333333"};
}
这样可以更好地扩展,因为您不必编写任何额外的代码来扩展,只需将条目添加到字典中(自动或其他方式)。使用
UIAlertViewDelegate
确实很笨拙。我建议每个人都使用任何非平凡的警报
使用此功能,代码变得简单且自包含
- (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];
}
使用
UIAlertViewDelegate
确实很笨拙。我建议每个人都使用任何非平凡的警报
使用此功能,代码变得简单且自包含
- (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];
}
此问题与Xcode无关。问题/问题是什么?此问题与Xcode无关。问题/问题是什么?