【问题标题】:create calling button in iPhone?在 iPhone 中创建呼叫按钮?
【发布时间】:2011-07-13 06:52:05
【问题描述】:

我想创建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");
    }
}

`

【问题讨论】:

    标签: iphone ios telephony


    【解决方案1】:

    应该这样做:

    #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");
        }
    }
    

    【讨论】:

      【解决方案2】:

      您总是使用 firstNumber 拨打电话。要区分按钮和警报视图,您可以将标签设置为警报视图。根据alertview的标签,你可以调用first或者second。

      【讨论】:

        【解决方案3】:

        您的号码是硬编码的,如下所示:

        #define firstnumber                 @"1"
        #define secondnumber                @"2"
        

        您可以通过添加 UITextField 并在警报视图中读取其值来读取数字。

        1. 在您的视图中创建一个 uitextview
        2. 编写委托方法并在控制器中创建一个 iboutlet
        3. 在您的警报视图回调中读取在 uitextfield 中输入的值并使用它来创建您的 url NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"tel://%@", textField.text]];

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-05-09
          • 2018-02-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多