2

我正在申请

1)我显示一个警报视图以接受或拒绝呼叫....

2) 但是,如果呼叫被呼叫者本身取消,则会显示一个警报,说明呼叫被呼叫者取消。

我的问题是,如果在我接受之前取消呼叫,警报视图会堆叠,并且在警报视图(2)附近我仍然可以看到警报视图(1),因为我的要求是直接在更接近任何警报视图。

我已经创建了一种生成警报视图的方法我给警报视图提供了差异标签

-(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

只需保留对警报视图的引用即可。这样,如果第一个仍在显示,您可以在显示第二个之前清除它。就像是:

.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 个 alertViews 显示一个在另一个顶部的可能性。

于 2012-05-21T06:48:53.523 回答