美文网首页
ios-Category相关

ios-Category相关

作者: Roger_单 | 来源:发表于2015-12-10 16:40 被阅读72次

    • Category 这是一种比继承更简洁的方法来对类进行扩展,不需创建子类就能为现有的类提供方法,它不但可以添加新方法,而且可以从写方法.
      Category 链接
    Category的缺点
    • 无法像类中添加新的实体变量 不能在 @interface 中添加实体变量

    • 名称冲突 (类别中的方法与原始类方法名称冲突时,类别具有更高的优先级。类别方法将完全取代初始方法从而无法再使用初始方法)

    Category的优点
    • 可以将类的实现分散到不同文件中 记住(#import)
    接下来我列举一下我项目中遇到的 Category
    • 加载网络图片 #import "UIImageView+AFNetworking.h" 用的是setImageWithURL
    [cell.image setImageWithURL:[NSURL URLWithString:dic[@"logoPath"]]];
    
    • 校验是否为有效的手机号#import "NSString+MALNSString.h"
    (![_TextFieldThree.text isMobilePhoneNumber])
    
    • 设置导航栏背景图片 ,和 Title#import "UIViewController+MALNavigation.h"
    [UIViewController setNavigationBarBgImage:self.navigationController andBgImage:[UIImage imageNamed:@"图片名字"]];
    [self setCenterItemWithTitle:@""];
    
    • 设置btn的点事件#import "UIViewController+MALNavigation.h"
    [barItem.contentBarItem addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    
    • 创建 Category@interface UIImageView_Bg : UIImageView
      把_删除在 Bg 处用()括住 把继承删除

    相关文章

      网友评论

          本文标题:ios-Category相关

          本文链接:https://www.haomeiwen.com/subject/fzkyhttx.html