简体   繁体   English

UIView不会添加到UIViewController

[英]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 首先在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? 如果没有互联网,就会一遍又一遍地调用该方法,但是为什么noIntenertView现在视图控制器上呢?

EDIT 编辑

Here is the CheckInternetView Class 这是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;

}

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. 由于您是在主UI线程上调用checkInternet并创建无限递归循环,因此没有时间更新UI。 It would only update after you return from viewDidLoad . 它只有在您从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. 若要解决此问题: checkInternet的调用移到后台线程中。 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. 在这里查看如何在主线程上再次运行需要的UI代码。

Note that you probably will end up with tons of CheckInternetView instances because you add them over and over again. 请注意 ,由于CheckInternetView添加它们,最终可能会产生大量CheckInternetView实例。 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. 最好每隔一秒钟或每隔半秒或在该范围内的某个时间间隔检查一次Internet。 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. 而且,您可能希望以连续循环而不是递归的方式进行操作,以减少对堆和堆栈的影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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