简体   繁体   中英

UIView doesn't add to UIViewController

I have a view that i display when there is no internet connection, and if there is no internet connection I check to see if it has be restored, with this method. Which is first called in 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;
        }
    }
}

And the method is called over and over again if there is no internet, but why isn't noIntenertView coming onto the view controller?

EDIT

Here is the CheckInternetView Class

#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;

}

Since you are calling the checkInternet on the main UI thread and create a infinite recursive loop there is no time for the UI to update. It would only update after you return from viewDidLoad . But that does not happen until you actually have a valid internet connection. If you do not have one, you will loop.

To fix this: move the call to checkInternet into a background thread. If you then need to present the view make sure that again happens on the main thread.

Take a look here to see how to properly create a new background thread. And here to see how to run the need UI code on the main thread again.

Note that you probably will end up with tons of CheckInternetView instances because you add them over and over again. It would be better to only create and add one if there is none yet present.

Additionally your code will be quite performance intensive and might even cause a crash because you just recurse without a proper timeout or break condition. It would be better to check the internet every second or every half second or some time interval in that range. And you might want to do it in a continuous loop rather than in a recursion to reduce the impact for the heap and stack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM