【问题标题】:Category is implementing a method which will also be implemented by its primary class ?Category 正在实现一个方法,该方法也将由其主类实现?
【发布时间】:2015-12-15 12:15:56
【问题描述】:

我在使用此代码时出现了这种减弱

-(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 

如果我这样使用有什么问题吗..

或者请给我确切的方法来覆盖该方法谢谢....

【问题讨论】:

  • 从技术上讲,它会起作用。但是,最好避免这种情况,因为从技术上讲,未定义将使用哪种实现的选择。
  • 有没有办法以正确的方式做到这一点@Avi
  • 子类化会更好。
  • 你能简单解释一下如何做到这一点@Avi :)

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


【解决方案1】:

对于非 UI 自定义,最好将其子类化。在您需要 drawPlaceholderInRect 的地方根据您的实现将 TextField 类分配为 CustomTextField

// 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

【讨论】:

  • 但它并没有改变占位符颜色怎么办兄弟@Muhammad
  • 你是用 CustomTextField 子类化 TextField 吗?
  • 您是在 Storyboard 还是 xib 中将 CustomTextField 设置为 TextField 的类?
  • 还有一件事,您在界面生成器中设置了任何占位符文本吗?
  • 不,我没有将此自定义类设置为我的文本字段兄弟
猜你喜欢
  • 2014-08-15
  • 2012-03-14
  • 2012-04-29
  • 2014-05-16
  • 1970-01-01
  • 2021-11-19
  • 2019-01-14
  • 1970-01-01
  • 2020-07-24
相关资源
最近更新 更多