Objective c 如何覆盖类别中的委托方法?

Objective c 如何覆盖类别中的委托方法?,objective-c,ios5,overriding,uialertview,objective-c-category,Objective C,Ios5,Overriding,Uialertview,Objective C Category,我已为UIAlertView创建了类别,并重写了委托方法willPresentAlertView,但类别中的我的方法未被激发 示例代码: @interface UIAlertView (CustomAlert) @end @implementation UIAlertView (CustomAlert) - (void)willPresentAlertView:(UIAlertView *)alertView1 { for (UIView *sub in [alertView1 su

我已为
UIAlertView
创建了类别,并重写了委托方法
willPresentAlertView
,但类别中的我的方法未被激发

示例代码:

@interface UIAlertView (CustomAlert)
@end

@implementation UIAlertView (CustomAlert)
- (void)willPresentAlertView:(UIAlertView *)alertView1
{

    for (UIView *sub in [alertView1 subviews])
    {
        if([sub class] == [UIImageView class])
        {
            ((UIImageView *)sub).image=nil;
            ((UIImageView *)sub).backgroundColor = [UIColor blackColor];
        }
    }

    [alertView1.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    [alertView1.layer setCornerRadius:0];
    [alertView1.layer setBorderWidth:2];
}

@end

如果您的目的是为
willPresentAlertView
delegate方法提供默认实现,那么您的方法(在
UIAlertView
类别中定义
willPresentAlertView
)是正确的。只需将警报本身作为委托传递,即可调用该方法:

 UIAlertView* alert = ...
 alert.delegate = alert;
如果不这样做,将不会调用委托方法


另一方面,我怀疑这样定义默认实现是否真的有用,因为原则上每个警报都需要自己的特定委托方法。

如果您的意图是为
willPresentAlertView
委托方法提供默认实现,那么您的方法(在
UIAlertView
类别中定义
willPresentAlertView
是正确的。您只需将警报本身作为委托传递,即可调用该方法:

 UIAlertView* alert = ...
 alert.delegate = alert;
如果不这样做,将不会调用委托方法

另一方面,我怀疑定义这样的默认实现是否真的有用,因为原则上每个警报都需要自己的特定委托方法。

简单:

@interface MONBlackStyle : NSObject // << new class, not category
+ (void)willPresentAlertView:(UIAlertView *)alertView1;
@end

@interface MONOrangeStyle : NSObject // << new class, not category
+ (void)willPresentAlertView:(UIAlertView *)alertView1;
@end
@interface-MONBlackStyle:NSObject/简单:

@interface MONBlackStyle : NSObject // << new class, not category
+ (void)willPresentAlertView:(UIAlertView *)alertView1;
@end

@interface MONOrangeStyle : NSObject // << new class, not category
+ (void)willPresentAlertView:(UIAlertView *)alertView1;
@end

@interface MONBlackStyle:NSObject//为什么要将其放在类别中?目的是什么?委托方法从委托对象运行,源对象只能启动方法,该方法将以委托对象所需的方式执行。如果需要其他功能,可以将其写在类别或子类中。如果希望实现委托方法,您可以在委托对象的类中执行。您是否已将警报视图设置为其自己的委托?为什么要在类别中使用它?目的是什么?委托方法是从委托对象运行的,源对象必须仅启动该方法,该方法将以委托对象希望的方式执行。如果您需要其他方法Nconationality,可以在category或subclass中编写。如果要实现委托方法,可以在委托对象的类中执行。是否已将警报视图设置为其自己的委托?