处理两个alertview

时间:2012-05-21 05:14:30

标签: iphone ios

我正在申请

1)我显示一个警报视图来接受或拒绝来电....

2)但是如果呼叫从呼叫者本身取消,则会显示一条警告,说呼叫被呼叫者取消。

我的问题是,如果在我接受之前取消了呼叫,警报视图就会堆叠起来,而在警报视图的附近(2),我仍然可以看到警报视图(1),我的要求是直接显示查看任何警报视图的近距离。

我创建了一个生成警报视图的方法,我将diff标签提供给alertview

  

- (void)generateMessage:(const char *)msg标题:(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 个答案:

答案 0 :(得分: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!
    }
  }
}

希望有帮助!

答案 1 :(得分:2)

您可以使用通知来实现此目的。当您意识到呼叫已被取消时,请发出通知。处理通知时,请关闭第一个UIAlertView:

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

处理&#34; CallCancelled&#34;通知:

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

- (void)handleCancelNotification:(id)object {
  // Dismiss the first AlertView.
  [alertView dissmissWithClickedButtonIndex:-1 animated:YES];
}

答案 2 :(得分: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];

    }
}

答案 3 :(得分:1)

实际上,重新设计应用程序要好得多,以避免2个alertViews在另一个应用程序的顶部显示一个。“/ p>