块枚举
// 字典和NSSet是无序的
NSDictionary *dic = @{@"test1" : @1, @"test2" : @2, @"test3" : @3, @"test4" : @4 };
[dic enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *obj, BOOL * _Nonnull stop) {
NSLog(@"key = %@", key);
NSLog(@"obj = %@", obj);
if ([obj isEqual:@3]) {
*stop = NO; // 当对象为3的时候就的时候就不执行 但是4会继续执行
}
}];
[[dic allKeys] enumerateObjectsUsingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"obj1 === %@",obj);
NSLog(@"index === %ld",idx);
}];
block传值
快速查询
@class LXKLoginParameterModel;
// 块:^ 申明的block变量名:parametreModelBlock 参数类型:LXKLoginParameterModel
typedef void(^parameterModelBlock)(LXKLoginParameterModel *);
// 返回值类型为NSString
typedef NSString *(^parameterModel)(LXKLoginParameterModel *);
@interface B2_LXKRegisterViewController : UIViewController
// 在栈中 所以使用copy
@property (nonatomic, copy) parameterModelBlock block;
@end
// 申明一个变量可以在快内修改
// __block phone = _parameterModel.phone;
// 成功才会有的回调
__weak typeof(self) weakSelf = self;
[LXKRegisterLoginViewModel userRegister:_parameterModel getUserIdCallback:^(NSString *userId) {
// 因为我在一个回调的block里所以才使用的weakSelf
NSAssert(weakSelf.parameterModel.phone && weakSelf.parameterModel.password, @"传递到登录页面的手机号或者密码为空");
if (weakSelf.block) {
weakSelf.block(weakSelf.parameterModel);
}
}];
__weak typeof(self) weakSelf = self;
registerVC.block = ^(LXKLoginParameterModel *model) {
// self的变量,合成了_parameterModel,可以不使用__block来修饰就可以改变
weakSelf.parameterModel.phone = model.phone;
weakSelf.parameterModel.password = model.password;
};
枚举的定义和循环
/**定义复用界面类型*/
typedef NS_ENUM(NSInteger, LXKRegisterMultiplexType) {
LXKRegisterMultiplexTypeRegister = 1 << 0, // 注册页面
LXKRegisterMultiplexTypeForgetPassword = 1 << 1, // 忘记密码
LXKRegisterMultiplexTypeModifyPassword = 1 << 2, // 修改登录密码页面
};
/**复用的界面类型 是基本数据类型所以用assgin*/
@property (nonatomic, assign) LXKRegisterMultiplexType multiplexType;
if (_multiplexType) {
switch (_multiplexType) {
case LXKRegisterMultiplexTypeRegister:
self.title = @"注册";
break;
case LXKRegisterMultiplexTypeForgetPassword:
self.title = @"忘记密码";
break;
case LXKRegisterMultiplexTypeModifyPassword:
self.title = @"修改密码";
break;
default:
break;
}
}
通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"circleUnreadMessageCount" object:self userInfo:@{@"messageCount" : @1}];
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshCircle:) name:@"circleUnreadMessageCount" object:nil];
协议
// 这个更具tag值来点击视图判断点击视图
@protocol LXKMeHeaderViewDelegate <NSObject>
@optional
- (void)LXKMeHeaderViewDelegate:(NSUInteger )viewTag;
@end
@interface LXKMeHeaderView : UIView
/**协议 根据tag值来判断点击的是哪一个*/
@property (nonatomic, weak) id <LXKMeHeaderViewDelegate> delegate;
@end
#pragma mark - action
- (void)avatarImageViewTap:(UITapGestureRecognizer *)sender {
if ([_delegate conformsToProtocol:@protocol(LXKMeHeaderViewDelegate)]) {
[_delegate LXKMeHeaderViewDelegate:[sender view].tag];
}
}
网友评论