在ViewController上显示SVProgressHUD?

时间:2016-09-28 07:45:26

标签: ios objective-c iphone swift swift3

我在AppDelegate中使用了Web服务。但我想在初始视图控制器(View Controller)上显示SVProgressHUD,并在Web服务完成后隐藏视图控制器中的SVProgressHUD。

2 个答案:

答案 0 :(得分:0)

我了解你的要求,我的建议是打包两个 class,HUDMysevers

我在这里有一个例子:

HUD.m

#import "HUD.h"
#import <UIKit/UIKit.h>
#import "MBProgressHUD.h"

@implementation HUD


 +(void)addHUD
{
    MBProgressHUD*  HUD = [[MBProgressHUD alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, IPHONE_HEIGHT)];

    HUD.tag = 5000;
    HUD.labelText = @"正在加载";

    [WINDOW addSubview:HUD];
    [HUD show:YES];
}

+(void)removeHUD
{
    MBProgressHUD*  HUD = (MBProgressHUD*)[WINDOW viewWithTag:5000];
    [HUD removeFromSuperview];

    HUD = nil;

}

@end

Mysevers.m

#import "Mysevers.h"
#import "AFNetworking.h"
#import "HUD.h"

@implementation Mysevers

+(void)AFPOSTAddressname:(NSString*)addressName parmas:(NSDictionary*)parmas RequestSuccess:(void(^)(id result))success failBlcok:(void(^)(void))failBlcok{

 [HUD addHUD];
    AFHTTPSessionManager *requestManager = [AFHTTPSessionManager manager];

    NSString *urlStr = [NSString stringWithFormat:@"%@%@",BASE_URL,addressName];
    NSLog(@"%@",urlStr);

/*[requestManager POST:urlStr parameters:parmas success:^(NSURLSessionTask *task, id responseObject) {
    [HUD removeHUD];
    success(responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"error==%@",[error localizedDescription]);
    [HUD removeHUD];
    failBlcok();
}];*/

[requestManager POST:urlStr parameters:parmas progress:^(NSProgress * _Nonnull uploadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    [HUD removeHUD];
    success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    if (error != nil) {
        NSLog(@"error==%@",[error localizedDescription]);
        [HUD removeHUD];
        failBlcok();
    }


    return ;
}];


}


  +(void)AFGETAddressname:(NSString*)addressName parmas:(NSDictionary*)parmas RequestSuccess:(void(^)(id result))success failBlcok:(void(^)(void))failBlcok{
    [HUD addHUD];
    AFHTTPSessionManager *requestManager = [AFHTTPSessionManager manager];

    NSString *urlStr = [NSString stringWithFormat:@"%@%@",BASE_URL,addressName];

NSLog(@"%@",urlStr);


/*[requestManager GET:urlStr parameters:parmas success:^(NSURLSessionTask *task, id responseObject) {

    [HUD removeHUD];
    success(responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"error==%@",[error localizedDescription]);

    [HUD removeHUD];
    failBlcok();
}];*/

[requestManager GET:urlStr parameters:parmas progress:^(NSProgress * _Nonnull downloadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    [HUD removeHUD];
    success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"error==%@",[error localizedDescription]);

    [HUD removeHUD];
    failBlcok();
}];



}
@end

<强> USAGE

您可以在项目中进行测试:

[Mysevers AFPOSTAddressname:url parmas:params RequestSuccess:^(id result) {
   // code here
}

希望这可以帮到你,这是我的经验。如果你想使用SVProgressHUD,你也可以替换MBProgressHUD

答案 1 :(得分:0)

SVProgressHUD上有一个名为

的静态方法。

+ (void)setContainerView:(UIView*)containerView;

[SVProgressHUD setContainerView:yourViewController.view];