我正在编写一种方法来使用来自Web服务的数据。我在一个类(公共类)中编写了这个方法,所以我可以在任何viewController
中使用这个类来使用数据。在这个方法中,我检查互联网连接。如果发生某些故障,我想显示带有错误描述的警报。这是我方法中的代码:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
mainSession = [NSURLSession sharedSession];
mainDataTask = [mainSession dataTaskWithRequest:MainRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
UIAlertController *alertOne = [UIAlertController alertControllerWithTitle:@"Something Wrong!" message:[error description] preferredStyle:UIAlertControllerStyleAlert];
// I want to show alert here
}
}];
});
这个方法在我的公共类(NSObject)中。如果连接有问题,我想在此块中显示错误。我怎么能这样做?
答案 0 :(得分:0)
#import <UIKit/UIKit.h>
然后你可以使用UIAlertController。
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"Title" message:nil preferredStyle:UIAlertControllerStyleAlert];
//...
id rootViewController=[UIApplication sharedApplication].delegate.window.rootViewController;
if([rootViewController isKindOfClass:[UINavigationController class]])
{
rootViewController=[((UINavigationController *)rootViewController).viewControllers objectAtIndex:0];
}
[rootViewController presentViewController:alertController animated:YES completion:nil];
答案 1 :(得分:0)
在主队列中执行AlertController声明,如下所示
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
mainSession = [NSURLSession sharedSession];
mainDataTask = [mainSession dataTaskWithRequest:MainRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertOne = [UIAlertController alertControllerWithTitle:@"Something Wrong!" message:[error description] preferredStyle:UIAlertControllerStyleAlert];
// I want to show alert here
});
}
}];
});
答案 2 :(得分:0)
使用以下代码。我希望它会对你有所帮助
//调用方法
[self showMessage:@"There is no internet connection for this device"
withTitle:@"Error"];
//写方法如
#pragma mark
#pragma mark -- UIAlertView Method
-(void)showMessage:(NSString*)message withTitle:(NSString *)title{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}]];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:alertController animated:YES completion:^{
}];
});
}
答案 3 :(得分:0)
如果您没有View Controller,如何使用UIAlertController呈现警报视图。 Description
是的,您只能在UIViewController类中使用UIAlertController。那么我们怎样才能在NSObject类中完成它。如果您看到上面给出的描述链接,您将得到答案。要总结以上描述:在当前窗口上方创建一个新窗口。这个新窗口将是我们显示警报的viewController。
因此,使用此viewController,您可以调用方法[presentViewController: animated: completion:]
。
dispatch_async(dispatch_get_main_queue(), ^{
UIWindow* window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
window.rootViewController = [UIViewController new];
window.windowLevel = UIWindowLevelAlert + 1;
NSString *msg=@“Your mssg";
UIAlertController* alertCtrl = [UIAlertController alertControllerWithTitle:@“Title" message:msg preferredStyle:UIAlertControllerStyleAlert];
[alertCtrl addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Yes",@"Generic confirm") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
// do your stuff
// very important to hide the window afterwards.
window.hidden = YES;
}]];
UIAlertAction *cancelAction= [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
window.hidden = YES;
}];
[alertCtrl addAction:cancelAction];
//http://stackoverflow.com/questions/25260290/makekeywindow-vs-makekeyandvisible
[window makeKeyAndVisible]; //The makeKeyAndVisible message makes a window key, and moves it to be in front of any other windows on its level
[window.rootViewController presentViewController:alertCtrl animated:YES completion:nil];
});
答案 4 :(得分:0)
使用下面的代码从NSObject类中展示UIAlertController。
[[[UIApplication sharedApplication] delegate].window.rootViewController presentViewController:alertViewController animated:true completion:nil];