iOS 网络请求失败View封装

封装一个NoData的View方便在网络请求失败时调用

1新建一个继承UIView的类:XSNoDataView

头文件里

#import <UIKit/UIKit.h>
typedef void (^ButtonBlock) (id sender);
@interface XSNoDataView : UIView

- (void)addButtonAction:(ButtonBlock)block;
@end

m文件里

#import "XSNoDataView.h"
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width
#define ScreenHeight  [UIScreen mainScreen].bounds.size.height

@interface XSNoDataView ()

@property (nonatomic, strong, nullable) ButtonBlock block;
@property (nonatomic, strong, nullable) UIButton *button;

@end

@implementation XSNoDataView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.frame = CGRectMake(ScreenWidth*3/8, adoptValue(240), ScreenWidth / 4, ScreenWidth / 4);
        [self addSubview:imageView];
        
        NSMutableArray *imgArray = [NSMutableArray array];
        for (int i=0; i<3; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"netError%d.png",i]];
            [imgArray addObject:image];
        }
        imageView.animationImages = imgArray;
        imageView.animationDuration = 6*0.15;
        imageView.animationRepeatCount = 0;
        [imageView startAnimating];
        
        
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(0, imageView.frame.origin.y+20+ScreenWidth/4, ScreenWidth, 30);
        label.textColor = [UIColor lightGrayColor];
        label.text = @"亲,您的手机网络不太顺畅喔~";
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        [self addSubview:label];
        
        UIButton *refreshBTN = [UIButton buttonWithType:UIButtonTypeCustom];
        [refreshBTN setTitle:@"重新加载" forState:UIControlStateNormal];
        [refreshBTN setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        refreshBTN.titleLabel.font = [UIFont systemFontOfSize:14];
        refreshBTN.layer.masksToBounds = YES;
        refreshBTN.layer.borderWidth = 1;
        refreshBTN.layer.cornerRadius = 3;
        refreshBTN.layer.borderColor = [UIColor lightGrayColor].CGColor;
        refreshBTN.frame = CGRectMake(ScreenWidth/2-50, label.frame.origin.y+60, 100, 30);
        [self addSubview:refreshBTN];
        [refreshBTN addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];

    }
    return self;
}

//实现block回调的方法
- (void)addButtonAction:(ButtonBlock)block {
    self.block = block;
}
- (void)buttonAction {
    if (self.block) {
        self.block(self);
    }
}
@end

2 在需要的UIViewController里导入这个类

在网络请求失败的方法里调用,block里就是点击刷新(重试)走的方法,如下:

@property (nonatomic, strong, nullable) XSNoDataView *baseView;

        if (self.baseView) {
            self.baseView.hidden = NO;
        }else{
            self.baseView = [[XSNoDataView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
            self.baseView.backgroundColor = [UIColor whiteColor];
            // self.baseView.frame = self.view.frame;
            [self.view addSubview:self.baseView];
            @weakify(self)//弱引用
            [self.baseView addButtonAction:^(id sender) {
                @strongify(self)
                self.baseView.hidden = YES;
                [self loadData];
            }];
            
        }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值