2

私はどこでアプリケーションを作っています

1)通話を受け入れるか拒否するためのアラートビューを表示します。

2)ただし、発信者自身からの通話がキャンセルされた場合は、発信者によって通話がキャンセルされたことを示すアラートが表示されます。

私の問題は、それを受け入れる前に通話がキャンセルされた場合、アラートビューがスタックされ、アラートビュー(2)のクローザーにアラートビュー(1)が表示されますが、私の要件はビューを直接表示することですアラートビューのいずれかに近い。

アラートビューを生成するメソッドを作成しました。アラートビューにdiffタグを付けます

-(void)generateMessage:(const char *)msg Title:(const char *)title withAcceptButton:(bool)doAddAcceptButton Tag:(int)tag {

dispatch_async(dispatch_get_main_queue()、^ {

                 // We are now back on the main thread
                 UIAlertView *alertView = [[UIAlertView alloc] >init];
                //add button

                 if(doAddAcceptButton==true)
                 {
                     [alertView  addButtonWithTitle:@"OK"];
                     [alertView addButtonWithTitle:@"Cancel"];
                     alertView.cancelButtonIndex=1;

                 }
                 else {
                     [alertView  addButtonWithTitle:@"OK"];
                     alertView.cancelButtonIndex=0;
                 }

                 //add tag
                 [alertView setTag:tag];

                 //add title
                 if(title==NULL)
                 {
                     [alertView setTitle:@"MESSAGE"];
                 }
                 else {
                     NSMutableString *head = [[NSMutableString >alloc] initWithCString:title
                                                                             >encoding:NSUTF8StringEncoding];
                     [alertView setTitle:head];
                     [head release];
                 }


                 if(msg==NULL)
                 {
                     [alertView setMessage:@"ERROR"];
                 }
                 else {
                     NSMutableString *body = [[NSMutableString >alloc] initWithCString:msg
                                                                             >encoding:NSUTF8StringEncoding];
                     [alertView setMessage:body];
                     [body release];
                 }
                 [alertView setDelegate:self];
                 [alertView show];
                 [alertView release];


             });

}

4

4 に答える 4

4

アラートビューへの参照を保持してください。そうすれば、最初のものがまだ表示されている場合は、2番目のものを表示する前にそれをクリアできます。何かのようなもの:

.hファイル:

@interface ViewController : UIViewController <UIAlertViewDelegate> {
  UIAlertView * _alertView1;
  UIAlertView * _alertView2;
}

.mファイル:

- (void)viewDidLoad; {
  [super viewDidLoad];
  _alertView1 = [[UIAlertView alloc] initWithTitle:@"Alert 1" message:@"A New call!" delegate:self cancelButtonTitle:@"Deny" otherButtonTitles:@"Accept", nil];
  _alertView2 = [[UIAlertView alloc] initWithTitle:@"Alert 2" message:@"The Call was cancelled!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
}

- (void)callWasCancelled; { // This would be the method where the second AlertView is called.
  if(_alertView1.isVisible){
    [_alertView1 dismissWithClickedButtonIndex:0 animated:YES];
  }
  [_alertView2 show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; {
  if(alertView == _alertView1){
    if(buttonIndex == 1){
      // Accept the call!
    }
  }
}

お役に立てば幸いです。

于 2012-05-21T05:25:33.843 に答える
2

これを実現するために通知を使用できます。通話がキャンセルされたことに気付いたら、通知を送信します。通知を処理するときは、最初のUIAlertViewを閉じます。

- (void)callCancelled {
    // Fire the notification
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CallCancelled" 
                                                        object:nil];   
}

「CallCancelled」通知を処理します。

[NSNotificationCenter defaultCenter] addObserver:self 
                                        selector:@selector(handleCancelNotification:) 
                                            name:@"CallCancelled" 
                                          object:nil];

- (void)handleCancelNotification:(id)object {
  // Dismiss the first AlertView.
  [alertView dissmissWithClickedButtonIndex:-1 animated:YES];
}
于 2012-05-21T05:23:40.787 に答える
1
- (void)viewDidLoad
{
    [super viewDidLoad];
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Confirm" message:@"Do you pick Yes or No?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
    [alert setDelegate:self];
    [alert show];
    [alert release];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0 && [alertView.title isEqualToString:@"Confirm"])
    {
        UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"Call is Cancelled" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert1 setDelegate:self];
        [alert1 show];
        [alert1 release];

    }
}
于 2012-05-21T05:32:57.753 に答える
1

実際には、2つのalertViewが一方を他方の上に表示する可能性を回避するために、アプリケーションを再設計することをお勧めします。

于 2012-05-21T06:48:53.523 に答える