Third-party Wait indicator. MBProgressHUD is a third-party Wait indicator framework. Below are the wait indicator styles provided by MBProgressHUD, which can be divided into two categories: Unknown End Time and known end time. In MBProgressHUD, you can add labels and detailed labels for the wait indicator.
We will download the MBProgressHUD from the source file. h and MBProgressHUD. m is copied to your project. The frameworks on which MBProgressHUD depends are: Foundation. framework, UIKit. framework and CoreGraphics. framework, we need to add these frameworks to the project.
Add the MBProgressHUD wait indicator to the application, and modify the startRequest method code of MasterViewController. m in the master View Controller as follows. Note the bold Section:
[Cpp]
-(Void) startRequest
{
// Initialize MBProgressHUD
MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo: self. view animated: YES];
Hud. mode = MBProgressHUDModeCustomView;
Hud. labelText = @ "Loading ";
NSString * strURL = [[NSString alloc]
InitWithFormat: @ "http: // iosbook3/mynotes/webservice. php"];
NSURL * url = [NSURL URLWithString: [strURL URLEncodedString];
NSString * post;
If (action = ACTION_QUERY) {// query processing
Post = [NSString stringWithFormat: @ "email =%@ & type =%@ & action =% @",
@ "<Your iosbook1.com user email>", @ "JSON", @ "query"];
} Else if (action = ACTION_REMOVE) {// Delete
NSIndexPath * indexPath = [self. tableView indexPathForSelectedRow];
NSMutableDictionary * dict = self. listData [indexPath. row];
Post = [NSString stringWithFormat: @ "email =%@ & type =%@ & action =%@ & id =% @",
@ "<Your iosbook1.com user email>", @ "JSON", @ "remove", [dict objectForKey: @ "ID"];
}
NSData * postData = [post dataUsingEncoding: NSUTF8StringEncoding];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url];
[Request setHTTPMethod: @ "POST"];
[Request setHTTPBody: postData];
NSURLConnection * connection = [NSURLConnection alloc]
InitWithRequest: request delegate: self];
If (connection ){
_ Datas = [NSMutableData new];
}
}
-(Void) connection :( NSURLConnection *) connection didFailWithError: (NSError *) error {
NSLog (@ "% @", [error localizedDescription]);
[MBProgressHUD hideHUDForView: self. view animated: YES];
}
-(Void) connectionDidFinishLoading: (NSURLConnection *) connection {
NSLog (@ "request completed ...");
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: _ datas
Options: NSJSONReadingAllowFragments error: nil];
If (action = ACTION_QUERY) {// query processing
[Self reloadView: dict];
} Else if (action = ACTION_REMOVE) {// Delete
NSString * message = @ "operation successful .";
NSNumber * resultCodeObj = [dict objectForKey: @ "ResultCode"];
If ([resultCodeObj integerValue] <0 ){
Message = [resultCodeObj errorMessage];
}
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "prompt message"
Message: message
Delegate: nil
CancelButtonTitle: @ "OK"
OtherButtonTitles: nil];
[AlertView show];
// Re-Query
Action = ACTION_QUERY;
[Self startRequest];
}
[MBProgressHUD hideHUDForView: self. view animated: YES];
}
-(Void) startRequest
{
// Initialize MBProgressHUD
MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo: self. view animated: YES];
Hud. mode = MBProgressHUDModeCustomView;
Hud. labelText = @ "Loading ";
NSString * strURL = [[NSString alloc]
InitWithFormat: @ "http: // iosbook3/mynotes/webservice. php"];
NSURL * url = [NSURL URLWithString: [strURL URLEncodedString];
NSString * post;
If (action = ACTION_QUERY) {// query processing
Post = [NSString stringWithFormat: @ "email =%@ & type =%@ & action =% @",
@ "<Your iosbook1.com user email>", @ "JSON", @ "query"];
} Else if (action = ACTION_REMOVE) {// Delete
NSIndexPath * indexPath = [self. tableView indexPathForSelectedRow];
NSMutableDictionary * dict = self. listData [indexPath. row];
Post = [NSString stringWithFormat: @ "email =%@ & type =%@ & action =%@ & id =% @",
@ "<Your iosbook1.com user email>", @ "JSON", @ "remove", [dict objectForKey: @ "ID"];
}
NSData * postData = [post dataUsingEncoding: NSUTF8StringEncoding];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL: url];
[Request setHTTPMethod: @ "POST"];
[Request setHTTPBody: postData];
NSURLConnection * connection = [NSURLConnection alloc]
InitWithRequest: request delegate: self];
If (connection ){
_ Datas = [NSMutableData new];
}
}
-(Void) connection :( NSURLConnection *) connection didFailWithError: (NSError *) error {
NSLog (@ "% @", [error localizedDescription]);
[MBProgressHUD hideHUDForView: self. view animated: YES];
}
-(Void) connectionDidFinishLoading: (NSURLConnection *) connection {
NSLog (@ "request completed ...");
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData: _ datas
Options: NSJSONReadingAllowFragments error: nil];
If (action = ACTION_QUERY) {// query processing
[Self reloadView: dict];
} Else if (action = ACTION_REMOVE) {// Delete
NSString * message = @ "operation successful .";
NSNumber * resultCodeObj = [dict objectForKey: @ "ResultCode"];
If ([resultCodeObj integerValue] <0 ){
Message = [resultCodeObj errorMessage];
}
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "prompt message"
Message: message
Delegate: nil
CancelButtonTitle: @ "OK"
OtherButtonTitles: nil];
[AlertView show];
// Re-Query
Action = ACTION_QUERY;
[Self startRequest];
}
[MBProgressHUD hideHUDForView: self. view animated: YES];
}