iOS开发技巧。

1,工程中创建一个Base控制器,其他控制器继承Base,在Base控制器里面注册一个通知,这样可以在子控制器发送通知,可以实现在任意界面接受消息。应用范围:断网通知,断开蓝牙,断开WiFi等。

2,分类是不能添加属性和成员变量的,但是可以通过其他方法实现。

声明:

#import <CoreBluetooth/CoreBluetooth.h>
#import <UIKit/UIKit.h>
@interface CBPeripheral (YFlalal)
@property (nonatomic, copy) NSString *animation;
@property (nonatomic, copy) NSString *mac;
@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) NSString *Photo;
@property (nonatomic, strong) NSString *userid;
@property (nonatomic, copy) NSString *isLost;
@property (nonatomic, copy) NSString *isBlind;
@property (nonatomic, copy) NSString *uid;
@property (nonatomic, strong) UIImage *iconImage;
@property (nonatomic, copy) NSString *connected;
@end

实现:

#import "CBPeripheral+YFlalal.h"
#import <objc/runtime.h>
static void * MyObjectMyCustomPorpertyKey = (void *)@"MyObjectMyCustomPorpertyKey";
static void * MyObjectMyCustomPorpertyMacKey = (void *)@"MyObjectMyCustomPorpertyMacKey";
static void * MyObjectMyCustomPorpertyNameKey = (void *)@"MyObjectMyCustomPorpertyNameKey";
static void * MyObjectMyCustomPorpertyPhotoKey = (void *)@"MyObjectMyCustomPorpertyPhotoKey";
static void * MyObjectMyCustomPorpertyUseridKey = (void *)@"MyObjectMyCustomPorpertyUseridKey";
static void * MyObjectMyCustomPorpertyUidKey = (void *)@"MyObjectMyCustomPorpertyUidKey";
static void * MyObjectMyCustomPorpertIconImageKey = (void *)@"MyObjectMyCustomPorpertIconImageKey";
static void * MyObjectMyCustomPorpertIsLostKey = (void *)@"MyObjectMyCustomPorpertIsLostKey";
static void * MyObjectMyCustomPorpertIsBlindKey = (void *)@"MyObjectMyCustomPorpertIsBlindKey";
static void * MyObjectMyCustomPorpertIsConnectedKey = (void *)@"MyObjectMyCustomPorpertIsConnectedKey";

@implementation CBPeripheral (YFlalal)
-(void)setConnected:(NSString *)connected{
    objc_setAssociatedObject(self, MyObjectMyCustomPorpertIsConnectedKey, connected, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)connected{
    return objc_getAssociatedObject(self, MyObjectMyCustomPorpertIsConnectedKey);
}
-(void)setIsBlind:(NSString *)isBlind{
    objc_setAssociatedObject(self, MyObjectMyCustomPorpertIsBlindKey, isBlind, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)isBlind{
    return objc_getAssociatedObject(self, MyObjectMyCustomPorpertIsBlindKey);
}
-(void)setIsLost:(NSString *)isLost{
    objc_setAssociatedObject(self, MyObjectMyCustomPorpertIsLostKey, isLost, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)isLost{
    return objc_getAssociatedObject(self, MyObjectMyCustomPorpertIsLostKey);
}
-(UIImage *)iconImage{
    return objc_getAssociatedObject(self, MyObjectMyCustomPorpertIconImageKey);
}
-(void)setIconImage:(UIImage *)iconImage{
     objc_setAssociatedObject(self, MyObjectMyCustomPorpertIconImageKey, iconImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(void)setUid:(NSString *)uid{
    objc_setAssociatedObject(self, MyObjectMyCustomPorpertyUidKey, uid, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)uid{
    return objc_getAssociatedObject(self, MyObjectMyCustomPorpertyUidKey);
}
-(void)setUserid:(NSString *)userid{
    objc_setAssociatedObject(self, MyObjectMyCustomPorpertyUseridKey, userid, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)userid{
    return objc_getAssociatedObject(self, MyObjectMyCustomPorpertyUseridKey);
    
}
-(void)setPhoto:(NSString *)Photo{
    objc_setAssociatedObject(self, MyObjectMyCustomPorpertyPhotoKey, Photo, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)Photo{
    return objc_getAssociatedObject(self, MyObjectMyCustomPorpertyPhotoKey);
}
-(void)setAnimation:(NSString *)animation{
    objc_setAssociatedObject(self, MyObjectMyCustomPorpertyKey, animation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)animation{
    return objc_getAssociatedObject(self, MyObjectMyCustomPorpertyKey);
}
-(void)setMac:(NSString *)mac{
    objc_setAssociatedObject(self, MyObjectMyCustomPorpertyMacKey, mac, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)mac{
    return objc_getAssociatedObject(self, MyObjectMyCustomPorpertyMacKey);
}
-(void)setName:(NSString *)name{
    objc_setAssociatedObject(self, MyObjectMyCustomPorpertyNameKey, name, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(NSString *)name{
    return objc_getAssociatedObject(self, MyObjectMyCustomPorpertyNameKey);
}

@end
这里引用例如OC的runtime机制,通过 objc_setAssociatedObject关联上一个属性。

3,block可以正向传值,也可以反向传值。

4,通知实现正向传递消息:众所周知通知要先注册才能收到消息,如果我要在一级界面发消息,二级界面响应怎门办。

这要重写二级界面的init方法。在init方法里面注册通知。

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self registNotification];
    }
    return self;
}
-(void)registNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setLocation) name:@"locating" object:nil];
}
一级界面发送通知在初始化二级界面后,跳转前。

YFMapViewController *mapView = [[YFMapViewController alloc] init];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"locating" object:nil];
        [self.navigationController pushViewController:mapView animated:YES];


4,持续更新中。。。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值