Ios 如何为SimplePing提供警报?
我有两个ViewController,一个是ViewController,另一个是GlobalViewController。我在GlobalVC中实现了SimplePing来检查网络连接。它工作正常,但我想在“没有互联网”时显示警报 这是我的密码Ios 如何为SimplePing提供警报?,ios,objective-c,swift,uiviewcontroller,uialertcontroller,Ios,Objective C,Swift,Uiviewcontroller,Uialertcontroller,我有两个ViewController,一个是ViewController,另一个是GlobalViewController。我在GlobalVC中实现了SimplePing来检查网络连接。它工作正常,但我想在“没有互联网”时显示警报 这是我的密码 //In ViewController.m - (void)viewDidAppear:(BOOL)animated { //Calling SimplePing 'start' function [_gc checkNetConnec
//In ViewController.m
- (void)viewDidAppear:(BOOL)animated {
//Calling SimplePing 'start' function
[_gc checkNetConnection];
}
//In GlobalVC
#pragma mark - SimplePingDelegate
- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error {
NSLog(@"didFailWithError: %@", [error description]);
NSLog(@"#####%@", error.localizedDescription);
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"No internet connection, please check it..." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancel];
[alert addAction:ok];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:YES completion:nil];
});
}
我得到了这个错误
Warning: Attempt to present <UIAlertController: 0x146045800> on <GlobalViewController: 0x145e0e510> whose view is not in the window hierarchy!
警告:尝试显示其视图不在窗口层次结构中的对象!
实际上,在这里我想将我当前的ViewController发送到present alert,但我不知道如何发送
任何人都可以用Swift或Objective c告诉我答案。这是因为您的GlobalViewController视图当前不在窗口中。当您导航到ViewController时,它已被替换 如果要在internet不可用时在其他每个viewcontroller中显示警报,则应将警报定义为UIViewController上的类别,然后在需要显示时调用它 大概是这样的: 选择项目,然后按“Cmd+n”添加新文件。选择Objective-C文件 输入文件名并保存 输入您的代码: UIViewController+SimplePing.h
@interface UIViewController (SimplePing)
- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error;
@end
UIViewController+SimplePing.m
@implementation UIViewController (SimplePing)
- (void)simplePing:(SimplePing *)pinger didFailWithError:(NSError *)error {
NSLog(@"didFailWithError: %@", [error description]);
NSLog(@"#####%@", error.localizedDescription);
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"No internet connection, please check it..." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancel];
[alert addAction:ok];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:alert animated:YES completion:nil];
});
}
@end
现在您可以从任何viewcontroller调用它。您能告诉我如何创建UIViewController+SimplePing.h/.m文件吗。选择项目,按cmd+n添加新文件。在iOS菜单中选择“Objective-C文件”。接下来在文件类型中选择“类别”,在类中选择“UIViewController”并输入文件名。@iOS我已经更新了答案,告诉您如何添加类别。@HAK,它没有显示警报。。。遇到同样的问题。非常感谢您的回答。@iOS在ViewDidDisplay:[自我提醒]中这样呈现它;