ios开发类似于qq的即时通讯功能中,如果网络断开,则要提示用户“当前网络连接失败”,那么uiview该如何设计呢,如下。

第一步:定义UIView属性。
@property (nonatomic, strong) UIView *networkStateView;
第二步:设计无网络状态的uiview
#pragma mark - Navigation 显示无网络状态
- (UIView *)networkStateView
{
if (_networkStateView == nil) {
_networkStateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 44)];
_networkStateView.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:199 / 255.0 blue:199 / 255.0 alpha:0.5];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, (_networkStateView.frame.size.height - 20) / 2, 20, 20)];
imageView.image = [UIImage imageNamed:@"messageSendFail"];
[_networkStateView addSubview:imageView];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 5, 0, _networkStateView.frame.size.width - (CGRectGetMaxX(imageView.frame) + 15), _networkStateView.frame.size.height)];
label.font = [UIFont systemFontOfSize:15.0];
label.textColor = [UIColor grayColor];
label.backgroundColor = [UIColor clearColor];
label.text = @"当前网络连接失败";
[_networkStateView addSubview:label];
}
return _networkStateView;
}第三步:在viewDidLoad方法中初始化前面定义的uiview属性networkStateView。
[self networkStateView];
第四步:在网络状态监听方法里面使用我们的uiview,设置UITableViewController里面自带的tableHeaderView属性就可以应用上面定义的networkStateView属性了。
#pragma mark 监听网络断开时网络状态提示
- (void)socketDisconnect:(NSNotification *)notification {
[UIView animateWithDuration:0.5 animations:^{
//无网络时提示
self.tableView.tableHeaderView = _networkStateView;
}];
}
#pragma mark 监听网络连接时网络状态提示
- (void)socketConnect:(NSNotification *)notification {
[UIView animateWithDuration:0.5 animations:^{
//有网络时不提示
self.tableView.tableHeaderView = nil;
}];
}