浅谈Objective-C类的通信

1.类的基本介绍

类的本属性介绍
//.h文件
#import 
#import "CommonAPI.h"
@interface classKnowlege : NSObject
@property(nonatomic,assign)int age;
@property(nonatomic,assign)float money;
@property(atomic,assign)BOOL flag;
@property(nonatomic,copy)NSString *nikename;
@property(nonatomic,weak)NSString *UI_weak;
@property(nonatomic,strong)NSString *UI_strong;
@property(nonatomic,retain)CommonAPI *api;

-(id)init;
-(id)initWithObj:(NSString *)str;

@end

//.m文件
#import "classKnowlege.h"

@implementation classKnowlege
-(id)init{
    self = [super init];
    if(self){
        NSLog(@"构造方法");
    }
    return self;
}
-(id)initWithObj:(NSString *)str{
    self = [super init];
    if(self){
        NSLog(@"%@",str);
    }
    return self;
}

@end
添加分类.png
//.h文件
/*分类,类扩展*/
#import "classKnowlege.h"

@interface classKnowlege (ChildClass)
-(void)run;
@end

//.m文件
#import "classKnowlege+ChildClass.h"

@implementation classKnowlege (ChildClass)
-(void)run{
    NSLog(@"run了");
}
@end

分类的作用,可以理解为实例父类调用子类的方法。

2.Blcok

//页面2
//.h文件
#import 
/*定义block*/
typedef void (^callback)(NSString *str);

@interface TwoViewController : UIViewController
/*定义个回调函数*/
-(void)callback:(callback)cb msg:(NSString *)str;
@end

//.m文件
/*block实例化*/
-(void)callback:(callback)cb msg:(NSString *)str{
    [self createLabel:str];
    cb(@"添加成功");
}
//页面1
-(void)toTwo:(UIViewController *)obj{
    
    TwoViewController *vc = [TwoViewController new];
    [vc callback:^(NSString *str) {
        NSLog(@"%@",str);
    } msg:@"213"];
    [self presentViewController:vc animated:YES completion:^{
        NSLog(@"123asdas");
    }];
}

3.通知

//接收通知的页面
.m文件
/*注册广播通知*/
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getNotification:) name:@"postUrl" object:nil];
/*实例化该方法*/
-(void)getNotification:(NSNotification *)notification{
    NSLog(@"%@",[notification.userInfo valueForKey:@"url"]);
    NSString *url = [notification.userInfo valueForKey:@"url"];
    [self createLabel:url];
}
//发送通知的页面
.m文件
TwoViewController *vc = [TwoViewController new];
                [self presentViewController:vc animated:YES completion:nil];
                NSString *url = @"http://team11.cn";
                /*发送广播通知*/
                [[NSNotificationCenter defaultCenter]postNotificationName:@"postUrl" object:nil userInfo:@{@"url":url}];

4.代理、协议

//协议头文件
#import 
@protocol LoginProtocol 
@optional
- (void)userLoginWithUsername:(NSString *)username password:(NSString *)password;
@end
//委托类.h文件
#import 
#import "LoginProtocol.h"
/**
 *  当前类是委托类。用户登录后,让代理对象去实现登录的具体细节,委托类不需要知道其中实现的具体细节。
 */
@interface DelegateViewController : UIViewController
// 通过属性来设置代理对象
@property(nonatomic,weak)iddelegate;

@end

//委托类.m文件
if([self.delegate respondsToSelector:@selector(userLoginWithUsername:password:)]){
                    [self.delegate userLoginWithUsername:@"jtr" password:@"123456"];
                }
//代理类.m文件
DelegateViewController *dvc = [[DelegateViewController alloc]init];
                dvc.delegate = self;
                [self presentViewController:dvc animated:YES completion:nil];
/**
 *  代理方实现具体登录细节
 */
- (void)userLoginWithUsername:(NSString *)username password:(NSString *)password {
    NSLog(@"username : %@, password : %@", username, password);
}

参考:

  • iOS 之 Protocol 详解
  • IOS开发之协议和代理
  • 你真的了解iOS代理设计模式吗?

5.KOV监听

/*注册kvo监听器*/
    _kvo = [[KVO_class alloc]init];
    [_kvo setMoney:20];
    [_kvo addObserver:self forKeyPath:@"money" options:NSKeyValueObservingOptionNew  context:nil];

/*监听触发的事件*/
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    NSString *money = [change valueForKey:@"new"];
    [self log:money];
}
-(void)log:(NSString *)msg{
    NSLog(@"%@=====",msg);
}

你可能感兴趣的:(浅谈Objective-C类的通信)