记录: 类目,延展,协议

1. 类目(Category)

    为原始的类添加新的方法来简便使用,例如:对 NSString  添加去除前后空格的 快捷方法

   

#import <Foundation/Foundation.h>

@interface NSString (Help)

/**
 *  去空白
 *
 */
- (NSString *)trimString;

@end

#import "NSString+Help.h"

@implementation NSString (Help)

- (NSString *)trimString
{
    //去除字符串 2端的空格
    return  [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
}

@end

2. 延展(Extension)

    定义自己的私有方法和属性,不能和外界共用

   

#import <UIKit/UIKit.h>
@class MKButton;
@interface ViewController : UIViewController

@end

@interface MKButton : UIButton

@property (assign, nonatomic) NSInteger flag;// 新属性

- (void)text; //新方法

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    MKButton * mk = [[MKButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
    mk.tag = 1;
    mk.flag = 105;
    mk.backgroundColor = [UIColor blueColor];
    [mk addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:mk];
}

- (void)click:(MKButton *)btn
{
    NSLog(@"%ld",btn.tag);
    NSLog(@"%ld",btn.flag);
    [self text];
}

- (void)text
{
    NSLog(@"您调用了私有方法");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

@implementation MKButton
- (void)text
{
    
}
@end



3.协议(Protocol)

   讲的是传值的作用和方法的调用,要用到两个文件名

#import <UIKit/UIKit.h>

@protocol SimpleProtocol <NSObject>

@required //必须遵守的协议[若不写 则是必须遵守的协议]

- (void)mustRealize:(NSString *)value;

@optional //可选的协议

- (void)maybeRealize:(NSString *)value;

@end

@interface SimpleView : UIView

@property (weak, nonatomic) id<SimpleProtocol>delegate;

- (void)chooseDelegate;

@end

#import "SimpleView.h"

@implementation SimpleView

- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {

        
    }
    return self;
}

- (void)chooseDelegate
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(maybeRealize:)]) {
        [self.delegate maybeRealize:@"可选协议传值"];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(mustRealize:)]) {
        [self.delegate mustRealize:@"必选协议传值"];
    }
}

@end


应用:

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController

@end

#import "ViewController.h"
#import "SimpleView.h"
@interface ViewController ()<SimpleProtocol>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    SimpleView *simple = [[SimpleView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    simple.backgroundColor = [UIColor redColor];
    simple.delegate = self;
    [simple chooseDelegate];
    [self.view addSubview:simple];
}

- (void)maybeRealize:(NSString *)value
{
    NSLog(@"%@",value);
}

- (void)mustRealize:(NSString *)value
{
    NSLog(@"%@",value);//必选协议传值 若是不写会有警告
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值