您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

MBProgressHUD 等待指示器實戰:為 App 加上漂亮的載入提示

編輯:iOS 相關資訊

等待指示器是行動應用裡不可或缺的元件。當使用者按下按鈕、App 卻需要向伺服器要資料時,一個清楚的「載入中」提示能大幅降低焦慮感。MBProgressHUD 正是社群中最常被使用的第三方等待指示器框架。

MBProgressHUD 提供的樣式大致可分為兩類:不知道結束時間的(持續轉圈)與已知結束時間的(含進度條)。此外它也支援為指示器加上標題與詳細說明文字。

整合到專案

將原始檔中的 MBProgressHUD.h 與 MBProgressHUD.m 複製到自己的工程,並補上它相依的三個框架:

  • Foundation.framework
  • UIKit.framework
  • CoreGraphics.framework

在網路請求中顯示 HUD

以下以主視圖控制器 MasterViewController.m 的 startRequest 方法為例,重點在於請求開始時建立 HUD、請求結束或失敗時將其隱藏。

-(void)startRequest {
    // 初始化 MBProgressHUD
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeCustomView;
    hud.labelText = @"Loading";

    NSString *strURL = [[NSString alloc] initWithFormat:@"https://iosbook3/mynotes/webservice.php"];
    NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];
    NSString *post;

    if (action == ACTION_QUERY) {          // 查詢
        post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@",
                @"<你的 iosbook1.com 用戶郵箱>", @"JSON", @"query"];
    } else if (action == ACTION_REMOVE) {  // 刪除
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSMutableDictionary *dict = self.listData[indexPath.row];
        post = [NSString stringWithFormat:@"email=%@&type=%@&action=%@&id=%@",
                @"<你的 iosbook1.com 用戶郵箱>", @"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(@"請求完成…");
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:_datas
                                                        options:NSJSONReadingAllowFragments
                                                          error:nil];
    if (action == ACTION_QUERY) {          // 查詢
        [self reloadView:dict];
    } else if (action == ACTION_REMOVE) {  // 刪除
        NSString *message = @"操作成功。";
        NSNumber *resultCodeObj = [dict objectForKey:@"ResultCode"];
        if ([resultCodeObj integerValue] < 0) {
            message = [resultCodeObj errorMessage];
        }
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示資訊"
                                                            message:message
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
        [alertView show];
        // 重新查詢
        action = ACTION_QUERY;
        [self startRequest];
    }
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

幾個實務提醒

第一,務必讓 hideHUDForView:animated: 在「成功」與「失敗」兩條路徑上都被呼叫,否則 HUD 會永遠停在畫面上,使用者也就再也按不到其他東西。

第二,刪除成功後直接呼叫 startRequest 重新載入,會導致 HUD 閃一下又出現。若在意這個細節,可以先隱藏 HUD、延遲一小段時間再觸發查詢。

第三,HUD 是加在 view 上的,因此請求進行中若視圖被銷毀,要注意不要對已釋放的 view 操作。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved