【问题标题】:Using Reachability throughout an application在整个应用程序中使用可达性
【发布时间】:2013-03-17 19:58:51
【问题描述】:

我想通过应用程序简单地使用可达性来检查互联网连接的可用性。当互联网连接丢失时,我想显示一个半透明的自定义视图(叠加层)(仍然可以看到后面显示的视图),其中包含一些文本,例如 ...Checking for Internet Connection。

这看起来很简单,但我无法按预期工作。

我已阅读有关可达性的苹果文档,并且日志似乎工作正常。我想确认我是否按预期正确设置了此设置,以及如何在连接恢复时生成透明视图/隐藏视图。

应用代理: 我打电话给[self setupReachability];

那我有这些方法:

- (void)setupReachability {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:ReachabilityChangedNotification object:nil];

    self.internetReach = [Reachability reachabilityForInternetConnection];
    [self.internetReach startNotifier];
    [self checkConnection:internetReach];

}

#pragma mark -
#pragma mark - Reachability 
//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note {
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    [self checkConnection: curReach];
}
-(void)checkConnection: (Reachability*) curReach {
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
        if (netStatus == NotReachable) {
            NSLog(@"inernet reach - not reachable");
        }
}

我知道我可以创建这样的模式视图:

 UIView *modalView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
 modalView.opaque = NO;
 modalView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];

UILabel *label = [[UILabel alloc] init];
label.text = @"Modal View";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.opaque = NO;
[label sizeToFit];
[modalView addSubview:label];

但我不确定如何将其添加到屏幕上当前显示的任何视图控制器/导航控制器?

编辑: 发生错误:

【问题讨论】:

    标签: ios ios5 uiview reachability


    【解决方案1】:

    [self.window.rootViewController presentModalViewController:myController animated:YES];

    您需要先将标签添加到模态视图控制器,然后以模态方式呈现它。

    【讨论】:

    • 我在尝试执行上述操作时遇到错误:*** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[UIView modalTransitionStyle]: unrecognized selector sent to instance 0xa4f6210' 还有一个警告在上面的问题中更新
    • 不要UIView 用于myController。将您的UIView 放入UIViewController
    • 好的,我设置为视图控制器,然后在控制器代码中添加视图代码,谢谢。
    猜你喜欢
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 2021-11-30
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多