【问题标题】:UIView doesn't add to UIViewControllerUIView 不添加到 UIViewController
【发布时间】: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


【解决方案1】:

由于您在主 UI 线程上调用 checkInternet 并创建无限递归循环,因此 UI 没有时间更新。它只会在您从viewDidLoad 返回后更新。但这在您真正拥有有效的互联网连接之前不会发生。如果你没有,你会循环。

要解决此问题:将对checkInternet 的调用移至后台线程。如果您随后需要呈现视图,请确保再次在主线程上发生。

看看here 看看如何正确地创建一个新的后台线程。和here 看看如何再次在主线程上运行需要的UI代码。

请注意,您可能会得到大量 CheckInternetView 实例,因为您一遍又一遍地添加它们。如果还没有,最好只创建和添加一个。

此外,您的代码将非常消耗性能,甚至可能导致崩溃,因为您只是在没有适当的超时或中断条件的情况下进行递归。最好每秒或每半秒或该范围内的某个时间间隔检查一次互联网。您可能希望在连续循环中而不是递归中执行此操作,以减少对堆和堆栈的影响。

【讨论】:

  • @jamesbrown 我包含了指向该问题的链接;)
  • 对,我解决了它添加一百万次视图的问题。并让后台线程工作。每秒检查一次左右的最简单方法是什么?为什么循环比递归更好?抱歉所有问题,但我非常感谢您的帮助:)
  • 只是休眠应该可以工作,因为您实际上现在有一个不同的线程正在操作:stackoverflow.com/questions/15004712/… 并且循环更好,因为递归与递归相比,您只需将更多的数据放在堆栈和堆上确实需要。
  • 嘿,最后一件事,我可以使用 while 循环来检查 while (!connection) 吗?这就是你的意思而不是递归
  • @jamesbrown 我建议while ((netStatus = [internetReach currentReachabilityStatus]) == NotReachable) { do your switch etc. }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
相关资源
最近更新 更多