ios - UIAlertView 委托(delegate)没有被调用?

标签 ios objective-c delegates uialertview

我有一个父类和 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/

相关文章:

ios - Xamarin-IOS-CLLocationManager : get location updates when iphone is locked or sleeping

ios - 通用 iOS 文件查找器

ios - 在运行时切换到FlatUIKit分段控件

iphone - 将 nsdictionary 转换为 nsdata

C# 调用 Action 跨线程访问

ios - 有效负载中的非公共(public)选择器

ios - 如果我从推送通知启动我的应用程序,为什么我的导航栏会消失?

c# - 无参数方法签名与参数委托(delegate)不匹配

使用 BeginInvoke 委托(delegate)的 C# 异步服务器

ios - UIButton 中的滑动手势