类别是否正在实施一种方法,该方法也将由其主要类实现?

时间:2015-12-15 12:15:57

标签: ios objective-c objective-c-category

使用此代码时,我的情况逐渐减弱

-(void) drawPlaceholderInRect:(CGRect)rect {
    // [[UIColor ] setFill];

    BOOL currentdevice=[[UIDevice currentDevice].model hasPrefix:@"iPhone"];
    if(currentdevice)
    {
        [[self placeholder] drawInRect:rect withAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"OpenSans" size:15] }];

    }
    else
    {

        [[self placeholder] drawInRect:rect withAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"OpenSans" size:20]}];
    }

}

要停止我正在使用此代码的警告消息

@implementation UITextField (placeholder)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"

// do your override
-(void) drawPlaceholderInRect:(CGRect)rect {
    // [[UIColor ] setFill];

    BOOL currentdevice=[[UIDevice currentDevice].model hasPrefix:@"iPhone"];
    if(currentdevice)
    {

        [[self placeholder] drawInRect:rect withAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"OpenSans" size:15] }];

    }
    else
    {

        [[self placeholder] drawInRect:rect withAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont fontWithName:@"OpenSans" size:20]}];
    }

}


#pragma clang diagnostic pop 

如果我像这样使用有任何问题..

或者请给我一个覆盖方法的确切方法谢谢......

1 个答案:

答案 0 :(得分:0)

对于非UI自定义,最好是子类。将TextField类指定为CustomTextField,根据您的实现需要drawPlaceholderInRect

// CustomTextField.h

    #import <UIKit/UIKit.h>

    @interface CustomTextField : UITextField

    @end

// CustomTextField.m

#import "CustomTextField.h"

@implementation CustomTextField

- (void)drawPlaceholderInRect:(CGRect)rect {

    BOOL currentdevice=[[UIDevice currentDevice].model hasPrefix:@"iPhone"];
    if(currentdevice){
        NSDictionary *attrDictionary = @{
                                         NSForegroundColorAttributeName: [UIColor redColor],
                                         NSFontAttributeName : [UIFont fontWithName:@"OpenSans" size:15.0]
                                         };

        [[self placeholder] drawInRect:rect withAttributes:attrDictionary];

    }
    else{

        NSDictionary *attrDictionary = @{
                                         NSForegroundColorAttributeName: [UIColor redColor],
                                         NSFontAttributeName : [UIFont fontWithName:@"OpenSans" size:20.0]
                                         };

        [[self placeholder] drawInRect:rect withAttributes:attrDictionary];
    }

  }
@end

Set Class as custom class