自定義view傳出點選方法,傳值
第一步:
@interface FLGHomeView : UIView
@property (nonatomic,copy)void (^noticeBlock)(NSString *noticeId);
@end
第二步:
(void)buttonAction:(UIButton*)button{
self.noticeBlock(@”123456”);
}
第三步:
FLGHomeView *home = [[FLGHomeView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
home.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:home];
home.noticeBlock = ^(NSString * _Nonnull noticeId) {
NSLog(@"-----------%@",noticeId);
};
一.block的四種形式
1.無引數,無返回值
void(^block)()=^(){
NSLog(@“今天正熱”);
};
block();
——->今天正熱
2.有引數,無返回值
void(^block)(int, int)=^(int a, int b){
int max=a>b?a:b;
NSLog(@“%d”,max);
};
block(10,20);
———>20
void(^block)(NSString *)=^(NSString *str){
NSLog(@“%@”,str);
};
block(@“nihao”);
———->nihao
3.有返回值,無引數
int (^block)()=^(){
return (int)100;
};
NSLog(@“%d”,block());
4.有引數,有返回值
NSString *(^block)(NSArray *,NSInteger)=^(NSArray *arr,NSIteger index){
return arr[index];
};
block(@[@“1”,@“2”,@“3”,@“4”],1);
———->2
block傳值
1.block 頁面之間反向傳值(陣列可以改為其他的型別,字串,字典等)(2頁面傳到1頁面)
@ 頁面1的跳轉方法中:
void(^block)(NSArray *)=^(NSArray *arr){
NSLog(@"%@",arr);
};<可以放在任何位置的>
- (void)click:(UIButton *)button{
secondViewController *secondVC=[[secondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
secondVC.block=block;
}
@頁面2.h中宣告屬性
@property(nonatomic,copy)void(^block)(NSArray *);
@頁面2返回方法中
-(void)click:(UIButton *)button{
[self.navigationController popViewControllerAnimated:YES];
self.block(@[@"1",@"2",@"3"]);
}
2.也可以不進行傳值 第二個頁面返回時需要在第一個頁面進行一些操作的時候可以使用block
@ 頁面1的跳轉方法中:
void(^block)()=^(){
// 從第二個頁面回來需要進行的操作
};
- (void)click:(UIButton *)button{
secondViewController *secondVC=[[secondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
secondVC.block=block;
}
@頁面2.h中宣告屬性
@property(nonatomic,copy)void(^block)();
@頁面2返回方法中
-(void)click:(UIButton *)button{
[self.navigationController popViewControllerAnimated:YES];
self.block();
}
headView按鈕的點選跳頁面方法
// 定義block
typedef void(^HomeTableBlock)(NSInteger index);
@property (nonatomic, copy) HomeTableBlock btnBlock;
// button的點選方法
(void)buttonAction:(UIButton *)btn {
if (self.btnBlock) {
self.btnBlock(btn.tag - 50000);
}
}
// 建立頭檢視
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
headView.btnBlock = ^(NSInteger index) {
switch (index) {
case 1:
{ break};
case 2:
{ break}
}
}
按鈕點選操作 實時返回資訊(視訊下載進度)
controller的按鈕
- (void)taoAction{
/// 只執行一次 block 多次返回資訊
views *view = [[views alloc] init];
[view tap:^(NSInteger a) {
NSLog(@"-------------%ld",a);
}];
}
views.h
@property (nonatomic, copy) void(^sblock)(NSInteger a);
- (void)tap:(void(^)(NSInteger a))blocksuccess;
views.m
(void)tap:(void(^)(NSInteger a))blocksuccess{
self.sblock = blocksuccess;
}
(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if(self){
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(tapaction) userInfo:nil repeats:YES];
}
return self;
}
(void)tapaction{
self.sblock(100);
}
自定義view傳出點選方法,傳值
第一步:
@interface FLGHomeView : UIView
@property (nonatomic,copy)void (^noticeBlock)(NSString *noticeId);
@end
第二步:
(void)buttonAction:(UIButton*)button{
self.noticeBlock(@”123456”);
}
第三步:
FLGHomeView *home = [[FLGHomeView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
home.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:home];
home.noticeBlock = ^(NSString * _Nonnull noticeId) {
NSLog(@"-----------%@",noticeId);
};
本作品採用《CC 協議》,轉載必須註明作者和本文連結