Jimmy G
Jimmy G

Reputation: 65

Multiple alertViews create error

Hello and good afternoon, I'm having some issues here, and to be honest, I don't understand I have to create different alertViews for the same screen with different messages, most of these alerts only have 1 button, but there's this one to delete that needs 2 buttons, the thing is that, since the others have only 1 button, when I created the 2 button screenview and I added the (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex method, I have some problems

some code here

- (IBAction)saveInfo{
if (med.text.length ==0) {
    UIAlertView *alertViewError = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ERROR",@"")
                                                        message:NSLocalizedString(@"EMPTY1",@"") 
                                                       delegate:self 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles: nil];
    [alertViewError show];
    [alertViewError release];
}
else if(medicamento.text.length >= 41){
    [self lenghtError:40:NSLocalizedString(@"TF_MED",@"")];
}
else if (med.text.length ==0 || descripcion.text.length == 0) {
    UIAlertView *alertViewError = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"ERROR",@"")
                                                        message:NSLocalizedString(@"EMPTY2",@"") 
                                                       delegate:self 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles: nil];
    [alertViewError show];
    [alertViewError release];
}
else if (descripcion.text.length >= 41){
    [self lenghtError:40:NSLocalizedString(@"TF_DESCRIPCION",@"")];
}
else{
    [self insertDictionary];
    UIAlertView *alertViewAcept = [[UIAlertView alloc] initWithTitle:@"" 
                                                        message: NSLocalizedString(@"ACCEPT_MSG",@"")
                                                       delegate:self 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles: nil];
    [alertViewAcept show];
    [alertViewAcept release];
    [self.navigationController popViewControllerAnimated:YES];
}

}

- (IBAction)cancelData{
UIAlertView *alertViewCancel = 
[[UIAlertView alloc] initWithTitle: NSLocalizedString(@"BT_DELETE_MED",@"")
                    message: NSLocalizedString(@"MSG_DELETE_MED",@"")
                   delegate:self 
          cancelButtonTitle:@"OK" 
          otherButtonTitles: @"Cancel", nil];
[alertViewCancel setTag:999];
[alertViewCancel show];
[alertViewCancel release];

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 999) {
    if(buttonIndex==0){
        [self.Bayer_DB_obj deleteRowWithKeyValue:[NSString stringWithFormat:@"%d",IdMed] onKeyName:@"id_ctl_med" onTable:@"ctl_med"];
        // code to delete here
        [self.navigationController popViewControllerAnimated:YES];
    }
}

}

So, in the first part, I created some alerts to indicate the user that he/she is making a mistake, in the second part, I need a confirmation before deletion, but here, I need 2 buttons, then, in the 3rd part, I have the method that is been called, I added a tag to my alert to avoid doing this comparison in all the alerts, the problem is that, when you show alertViewAcept, it takes you to the previous view controller, and after you click the ok button (that actually is the cancelbuttontitle) the app crashes without any "error message"

so I'm not sure what I'm doing wrong, please help

Upvotes: 2

Views: 313

Answers (1)

iTukker
iTukker

Reputation: 2083

My guess the problem is that you set the delegate for the alertViewAcept, and right after you showed the alert, you pop the viewController and so your delegate will get released, which will then give you an error once a button on the alert view is clicked.

You should do this:

UIAlertView *alertViewAcept = [[UIAlertView alloc] initWithTitle:@"" 
                                                        message: NSLocalizedString(@"ACCEPT_MSG",@"")
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles: nil];

Even better, all your alerts which only have the OK button, do not need a delegate. And in that case you do not even need the tag.

Upvotes: 1

Related Questions