我想创建 2 个按钮。当按钮被按下时,它应该会打电话。我写了这段代码,它可以工作,但我的问题是按钮调用相同的号码。我想调用不同的号码。 谁能修复我的代码?
#define firstnumber @"1"
#define secondnumber @"2"
- (IBAction)first:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:firstnumber message:@"do you want to call?" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show];
[alert release];
}
- (IBAction)second:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:secondnumber message:@"do you want to call?" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user cticlicked one of the OK/Cancel buttons
if (buttonIndex == 1)
{
NSLog(@"ok");
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"tel://%@", firstnumber]];
[[UIApplication sharedApplication] openURL:url];
[url release];
}
else
{
NSLog(@"cancel");
}
}
`
最佳答案
应该这样做:
#define firstnumber @"1"
#define secondnumber @"2"
- (IBAction)first:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:firstnumber message:@"do you want to call?" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alert.tag = 1;
[alert show];
[alert release];
}
- (IBAction)second:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:secondnumber message:@"do you want to call?" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"ok", nil];
alert.tag = 2;
[alert show];
[alert release];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user cticlicked one of the OK/Cancel buttons
if (buttonIndex == 1)
{
NSLog(@"ok");
NSURL *url;
switch (actionSheet.tag) {
default:
case 1:
url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"tel://%@", firstnumber]];
break;
case 2:
url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"tel://%@", secondnumber]];
break;
}
[[UIApplication sharedApplication] openURL:url];
[url release];
}
else
{
NSLog(@"cancel");
}
}
关于iphone - 在 iPhone 中创建调用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6675127/