block 做参数

时间:2021-03-15 00:46:18

三部分

1,定义函数

/* 传出类定义block

*/

//定义block

typedef void (^ItemClickBlock)(NSInteger selectedIndex);

//block 做函数参数

+ (instancetype)navigationViewWithItems:( NSArray <NSString *> * )items itemClick:(ItemClickBlock)itemClick;

2,实现

#pragma mark - init block 做参数,实现方法, --

+ (instancetype)navigationViewWithItems:(NSArray<NSString *> *)items itemClick:(ItemClickBlock)itemClickBlock{

WSNavigationView *nav = [[WSNavigationView alloc] init];

nav.btns = [NSMutableArray arrayWithCapacity:items.count];

//

NSLog(@"  navigationViewWithItems 初始化 ----------- ");

nav.itemClickBlock = itemClickBlock;

nav.items = items;

////禁用滚动到最顶部的属性

nav.scrollsToTop = NO;

NSLog(@"-=====--------==---==== ");

return nav;

}

3,调用

//添加导航view

typeof(self) __weak weakObj= self;

//调用 block做参数,方法。  - 传入   意义在于:当点击时,传入信息到此。

WSNavigationView *view = [WSNavigationView navigationViewWithItems:nil itemClick:^(NSInteger selectedIndex) {

//实现

NSLog(@"2- WSContainerController init   实现  传入 点击 %lu ",  selectedIndex);

//传入 弱引用

//  [weakObj setSelectedIndex:selectedIndex];

[self setSelectedIndex:selectedIndex];

}];

//////////////////////////数据库使用

1,实现

-(void)LoadAllCityWithCompletion:(XNDLoadAreaModelCompletion)completion;

2,实现

-(void)LoadAllCityWithPid:(int)pid Completion:(XNDLoadAreaModelCompletion)completion{

[[[DBManager alloc]init] loadCityArrayAllWithPid:pid Completion:^(NSArray *messages, NSError *error) {

self.modelCityArry=[[NSMutableArray alloc]initWithArray: messages ];

completion(nil,nil);

}];

}

3.调用

//调用 block做参数的方法 传入

[[XNDAreaModule instance] LoadAllCityWithPid:pid Completion:^(NSArray *messages, NSError *error) {

if (isPosted==NO) {

[[NSNotificationCenter defaultCenter] postNotificationName:KNotifictionChangeArea object:@"1"];

}

}];