【问题标题】:UIAlertView delegate not getting called?UIAlertView 委托没有被调用?
【发布时间】:2015-06-08 08:17:54
【问题描述】:

我有一个父类和 5 个子类。在这 5 个中,有 2 个类(子)我使用了 AlertView 并使用了警报委托方法。 在子类中,我有一个通用按钮,通过该按钮调用父类的警报委托。这是“注销”按钮。通过哪个父类的警报委托可以访问。所以会发生什么是我使用警报委托的类(在我的情况下有 2 个这样的类)父类的委托没有被调用。但是我没有使用来自那些类父类的委托的警报视图的类被调用。 我不知道我是否有道理,如果没有,请告诉我,我会再次尝试更好地解释。

//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];
    }
}

【问题讨论】:

  • 可能是parent被释放了
  • 将 - (void)dealloc { NSLog(@"%@", self);} 添加到 ParentClass,你会发现它被释放了
  • 这适用于 iOS 8 吗?如果是这样,您无论如何都应该使用 UIAlertController。

标签: ios objective-c delegates uialertview


【解决方案1】:

从子类中移除alertView:clickedButtonAtIndex: 方法。

如果您的第 2 类是第 1 类(父类)的子类,则无需在子类中重新实现父类方法,除非您要添加一些额外的功能。因为第二类已经继承了所有的父类方法。

所以,如果你从子类中删除委托方法,父类方法会自动被调用。

【讨论】:

  • 您能否更新您的问题以显示父类和子类的最终代码。另外我不明白你为什么在子类中创建父类对象。看到整个代码就会清楚。
【解决方案2】:

<UIAlertViewDelegate> 确认父类而不是子类,然后将在父类及其所有子类中调用alertView 委托。请确保,您不要再次使用 <UIAlertViewDelegate> 再次确认子类

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    如果您同时定义 alertView:clickedButtonAtIndex: 委托方法,即超类和子类,我遇到了同样的问题,那么将不会调用超类方法,因为您必须从子类中删除委托方法.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-22
      • 2010-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-08
      • 1970-01-01
      相关资源
      最近更新 更多