Saraswati
Saraswati

Reputation: 1526

handling two alertview

I am making an application where

1) i show an alert view to accept or deny the call....

2) But if the call is canceled from the caller itself then an alert is shown saying the call is cancelled by the caller.

My problem is if the call is canceled before i accept it the alert view are stacked and on the closer of the alert view(2) i still can see the alert view(1) where as my requirement is to directly show the view on the closer of any of the alertview.

i have created a method to generate alert view i give diff tags to the alertview

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


             });

}

Upvotes: 2

Views: 211

Answers (4)

Maksim
Maksim

Reputation: 2052

Actually, it is much better to redesign your application to avoid the possibility of 2 alertViews showing one on the top of the other one.

Upvotes: 1

Anil Kothari
Anil Kothari

Reputation: 7733

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

    }
}

Upvotes: 1

msgambel
msgambel

Reputation: 7340

Just keep a reference to the alert views. That way, if the first is still showing, you can clear it before showing the second. Something like:

.h file:

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

.m file:

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

Hope That Helps!

Upvotes: 4

melsam
melsam

Reputation: 4977

You could use Notifications to achieve this. When you realize that the call has been cancelled, fire a notification. When handling the notification, dismiss the first UIAlertView:

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

Handle the "CallCancelled" notification:

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

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

Upvotes: 2

Related Questions