2011-07-13 5 views
1

2ボタンを作成したいと思います。ボタンを押すと電話がかかります。私はこのコードを書いて動作しますが、問題はボタンが同じ番号を呼び出すことです。私は別の番号を呼びたいと思っています。 誰でも自分のコードを修正できますか?iPhoneで通話ボタンを作成しますか?

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

`

答えて

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

感謝の伴侶を。それはもう動作します:) – muhammedkasva

0

下図のようにあなたの番号がハードコードされています

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

あなたはUITextFieldのを追加し、あなたのアラートビューにその値を読み取ることによって、番号を読み取ることができます。

  1. デリゲートメソッドを書くビュー
  2. でuitextviewを作成し、コントローラにiboutletを作成アラートビューのコールバックで
  3. はUITextFieldのに入力された値を読み、あなたのURLを作成するために使用NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"tel://%@", textField.text]];
1

あなたはfirstNumberを使用して電話をかけています。ボタンとアラートビューを区別するために、タグをアラートビューに設定できます。アラートビューのタグによれば、あなたは1番か2番に電話をかけることができます。

関連する問題