iphone - 在 iPhone 中创建调用按钮?

标签 iphone ios telephony

我想创建 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/

相关文章:

iphone - OpenGL ES 1.1 : How to change texture color without losing luminance?

php - 将数据从 iPhone 应用程序传递到 PHP 文件

ios - 同步执行多个动画的最佳方式

iphone - 根据 iOS 版本有条件地包含 header

ios - UIView 动画导致 UIButton 跳转

iphone - 在ios5中访问相机 View 并捕获图像使用哪个框架?

iphone - 如何创建在选项卡之间切换时保留在屏幕上的 View ?

java - 如何在 PCI 调制解调器上有来电时获取电话号码

asterisk - Asterisk 中的自定义 IVR 操作

android - 支持 GSM 的 AVD 中没有电话功能