我有一个父类和 5 个子类。在这 5 个类中,有 2 个类(子)我使用了 AlertView 并使用了警报委托(delegate)方法。 在子类中,我有一个通用按钮,通过该按钮调用父类的警报委托(delegate)。这是“注销”按钮。通过它可以访问父类的警报委托(delegate)。所以发生的是我使用警报委托(delegate)的类(在我的例子中有 2 个这样的类)父类的委托(delegate)没有被调用。但是我没有使用这些类父类的委托(delegate)的警报 View 的类正在被调用。 我不知道我是否有道理,如果没有的话请告诉我,我会再次尝试更好地解释。
//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];
}
}
最佳答案
从子类中删除 alertView:clickedButtonAtIndex:
方法。
如果你的第二类是第一类(父类)的子类,你不需要在子类中重新实现父类的方法,除非你要添加一些额外的功能。因为2nd类已经继承了父类的所有方法。
因此,如果您从子类中移除委托(delegate)方法,父类方法将自动被调用。
关于ios - UIAlertView 委托(delegate)没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30704207/