IOS development-use MBProgressHUD to increase user experience (2)
In the previous blog "iOS development-using MBProgressHUD to increase User Experience", I used MBProgressHUD encapsulated by others for loading prompts, which is quite convenient. Today, we use the native MBProgressHUD third-party library on Github for loading prompts, which is a little bit more complicated than someone else has encapsulated. The Code has been uploaded to: https://github.com/chenyufeng1991/usembprogresshud. The implementation steps are as follows:
(1) The network request number is also used for local implementation. (Please refer to the notes)
-(IBAction) sourceButtonPressed :( id) sender {// declare object; MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo: self. view animated: true]; // displayed text; hud. labelText = @ loading; AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager]; manager. responseSerializer = [AFHTTPResponseSerializer serializer]; // if you change it to POST, you can perform the POST request. // put the parameters to be passed directly into the URL, instead of in the dictionary. [manager GET: @ http://webservice.webxm L.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo? MobileCode = 18888888888 & userId = parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject) {NSString * string = [[NSString alloc] initWithData: responseObject encoding: encoding]; NSLog (@ success: % @, string); // load successfully. First, remove the original HUD; hud. removeFromSuperViewOnHide = true; [hud hide: true afterDelay: 0]; // a success prompt is displayed. MBProgressHUD * successHUD = [MBProgressHUD showHUDAddedTo: self. view animated: true]; successHUD. labelText = @ loaded successfully; successHUD. mode = MBProgressHUDModeCustomView; // custom view; // you can set the corresponding image; successHUD. customView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ success]; successHUD. removeFromSuperViewOnHide = true; [successHUD hide: true afterDelay: 1];} failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {NSLog (@ failure: % @, error ); hud. removeFromSuperViewOnHide = true; [hud hide: true afterDelay: 0]; // display the failure prompt; MBProgressHUD * failHUD = [MBProgressHUD showHUDAddedTo: self. view animated: true]; failHUD. labelText = @ loading failed; failHUD. mode = MBProgressHUDModeCustomView; failHUD. customView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: @ error]; failHUD. removeFromSuperViewOnHide = true; [failHUD hide: true afterDelay: 1] ;}];}
(2) The display effect is as follows:
Loading]
.
[Loaded successfully]
.
[Loading failed]
.
It can be seen that using the default HUD requires a lot of code. If you are interested, you can encapsulate it yourself. This greatly simplifies the amount of code.