【发布时间】:2015-09-06 15:16:19
【问题描述】:
我有一个在没有互联网连接时显示的视图,如果没有互联网连接,我会使用这种方法检查它是否已恢复。哪个首先在viewDidLoad中调用
-(void)checkInternet {
internetReach = [Reachability reachabilityForInternetConnection];
[internetReach startNotifier];
NetworkStatus netStatus = [internetReach currentReachabilityStatus];
switch (netStatus){
case ReachableViaWWAN:{
isReachable = YES;
NSLog(@"4g");
noInternetView.hidden = YES;
break;
}
case ReachableViaWiFi:{
isReachable = YES;
noInternetView.hidden = YES;
NSLog(@"wifi");
break;
}
case NotReachable:{
NSLog(@"NONE");
noInternetView = [[CheckInternetView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:noInternetView]; //IT IS NOT ADDED??
isReachable = NO;
[self checkInternet];
break;
}
}
}
如果没有互联网,该方法会被一遍又一遍地调用,但是为什么noIntenertView 没有进入视图控制器?
编辑
这里是 CheckInternetView 类
#import "CheckInternetView.h"
@implementation CheckInternetView
- (id)initWithFrame:(CGRect)frame {
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
self = [super initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
if (self) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 60)];
label.textAlignment = NSTextAlignmentCenter;
label.numberOfLines = 2;
[label setCenter:CGPointMake(self.frame.size.width / 2 , self.frame.size.height / 2 - 25)];
label.text = @"You've lost your internet connection. Please connect to internet.";
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
spinner.frame = CGRectMake(0, 0, 80, 80);
[spinner setCenter:CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2 + 40)];
[spinner startAnimating];
label.textColor = whiteColorAll;
spinner.color = whiteColorAll;
[self setBackgroundColor:[UIColor blackColor]];
[self addSubview: spinner];
[self addSubview: label];
}
return self;
}
【问题讨论】:
-
登录
noInternetView时显示什么? -
你试过'bringSubviewToFront:'吗?
-
是在调用
[super viewDidLoad]之前还是之后调用了checkInternet? -
@rebello95 当我登录时
noInternetVIew我得到<CheckInternetView: 0x126122410; frame = (0 0; 320 568); layer = <CALayer: 0x174828f20>> -
@JAL
bringSubViewToFront不起作用
标签: ios objective-c uiview reachability