alertView:clickedButtonAtIndex: causes program closing
I have this piece of code which allocates 2 UIAlertView :
promptConnexion = [[UIAlertView alloc] initWithTitle:@"Connexion"
message:@"\n\n\n"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
promptConnexion.tag = 1;
promptInscription = [[UIAlertView alloc] initWithTitle:@"Inscription"
message:@"\n\n\n\n\n"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK", nil];
promptInscription.tag = 2;
and this one which specifies what to do when buttons are pressed :
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1)
{
if (buttonIndex == 1)
{
开发者_如何学JAVA NSLog(@"Connexion button OK");
}
}
if (alertView.tag == 2)
{
if (buttonIndex == 1)
{
NSLog(@"Inscription button OK");
}
}
}
In my console I actually get "Connexion button OK" and "Inscription button OK" when I should to.
But right after my program is closing unexpectedly whithout any warning or error in my console.
Moreover, if I don't overload alertView:clickedButtonAtIndex: my UIAlertView is just closing, my app still runs correctly, but my buttons do nothing else (obviously).
I also wrote this code :
and showed my 2 UIAlertView this way :
[promptConnexion show]; [promptConnexion release];
and :
[promptInscription show]; [promptInscription release];
and overloaded alertView:didDismissWithButtonIndex: too :
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { [alertView release]; }
What am I doing wrong?
Here is your problem;
[alertView release];
You don't want to be doing that at all. In fact if that is all you are doing inside the method, you can remove the method completely as well. This is causing a double release of the alert view which you have already released using [promptConnexion release] etc.
One way to detect such things is to run your program with NSZombiesEnabled as that can show you when you try to release an object that is already released.
This code is your problem:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex: (NSInteger)buttonIndex
{
[alertView release]; // Should not be released here!
}
The reason is, you have already released the alertView here:
[promptConnexion release];
[promptInscription release];
You have done your "duty" by releasing the object you have allocated. The alert view object is now under the responsibility of the operating system displaying that alert view, and it will release it when necessary, without any action of your part.
Just remove that code and it should work.
Also, you should read Apple's Memory Management Rules to have a better understanding of this issue
[promptInscription release];
and [promptConnexion release];
already releases the alertViews. You are releasing it and then after accessing the tag for those alert views and then release it again which I think is the reason for crash.
you should not release the alertView in
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
you already release the alertView instances so there is no need to release that..
Try with not release your alertView in below function.
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex`
SO your function should be
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
//Put you code here
}
Just do one thing write your code as below
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1)
{
if (buttonIndex == 1)
{
NSLog(@"Connexion button OK");
}
}
if (alertView.tag == 2)
{
if (buttonIndex == 1)
{
NSLog(@"Inscription button OK");
}
}
}
No need to override clickedButton Index method just remove it
精彩评论