Rahul Rahul - 13 days ago 9
iOS Question

UIAlertView delegate not getting called?

I have a parent class and 5 sub classes. Out of these 5 there are 2 classes(sub) where I have used AlertView and have used alert delegate method.
In sub classes I have one common button through which parent class' alert delegate is getting called. It's "Logout" button. Through which parent class' alert delegate is accessible. So what happens is the classes where I have used alert delegate (in my case there are 2 such classes) the parent class' delegate is not getting called . But the class where I haven't use alert view from those classes parent class' delegate is getting called.
I don't know whether I am making sense or not, if not then let me know I will try again to explain better.

//parent class
-(void)someMethod
{
UIAlertView * alertView =[[UIAlertView alloc]initWithTitle:ALERT_TITLE message:@"Are you sure want to logout?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];

[alertView show];
}

// not getting called "only" when I have alert view in my sub class
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
[[AppDelegate appDelegate]logout];
[[AppDelegate appDelegate]saveWelComeBool:YES];
}
}


//sub class
ParentClass *parent = [[ParentClass alloc]init];
[parent someMethod];// method is getting called but not the alert delegate

-(void)someMethod2
{
UIAlertView * alertView =[[UIAlertView alloc]initWithTitle:ALERT_TITLE message:@"Are you sure want to logout?" delegate:self cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];

[alertView show];
}

//getting called
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
[[AppDelegate appDelegate]logout];
[[AppDelegate appDelegate]saveWelComeBool:YES];
}
}

Answer

Remove the alertView:clickedButtonAtIndex: method from the subclass.

If your 2nd class is subclassing the 1st class (Parent class), you don't need to re-implement the parent class methods in the subclass, unless you are adding some extra functionality. As the 2nd class has already inherited all the parent class methods.

So, if you remove the delegate method from the subclass, the parent class method will automatically be called.