您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 開發(60):用 GCD 執行 UI 相關操作

編輯:iOS 開發綜合

前言

iOS 有個重要的規則:所有與 UI 相關的操作都必須在主執行緒(主隊列)上進行。Grand Central Dispatch 提供了一個便捷的方式,讓我們把更新介面的程式碼派送到主隊列執行。本文作為入門,示範如何使用 GCD 在主隊列上彈出一個提示框。

程式碼實例

ZYViewController.h

#import <UIKit/UIKit.h>

@interface ZYViewController : UIViewController

@end

ZYViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // dispatch_queue_t:分派隊列是一個輕量級物件,用於提交後續要執行的區塊。
    // 這裡取得主隊列。
    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    /*
     以非同步方式把區塊提交到隊列並立即返回:
     - 第一個參數:要提交區塊的目標隊列,由系統保留直到區塊執行完畢,不可為空。
     - 第二個參數:要提交的區塊,函式會複製並在執行後釋放,不可為空。
    */
    dispatch_async(mainQueue, ^(void)
    {
        [[[UIAlertView alloc] initWithTitle:@"GCD"
                                   message:@"GCD is amazing!"
                                  delegate:nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil, nil] show];
    });
}

ZYAppDelegate.h

#import <UIKit/UIKit.h>

@class ZYViewController;

@interface ZYAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ZYViewController *viewController;

@end

ZYAppDelegate.m

這裡改用 C 函式搭配結構體傳遞參數,示範 dispatch_async_f 的用法:

// 宣告承載提示框資料的結構體
typedef struct{
    char *title;
    char *message;
    char *cancelButtonTitle;
} AlertViewData;


@implementation ZYAppDelegate

// C 函式:建立並顯示提示框
void displayAlertView(void *paramContext){
    AlertViewData *alertData = (AlertViewData *)paramContext;
    NSString *title = [NSString stringWithUTF8String:alertData->title];
    NSString *message = [NSString stringWithUTF8String:alertData->message];
    NSString *cancelButtonTitle =
        [NSString stringWithUTF8String:alertData->cancelButtonTitle];
    [[[UIAlertView alloc] initWithTitle:title
                                message:message
                               delegate:nil
                      cancelButtonTitle:cancelButtonTitle
                      otherButtonTitles:nil, nil] show];
    free(alertData);
}

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // 取得主執行緒隊列
    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    AlertViewData *context = (AlertViewData *) malloc(sizeof(AlertViewData));
    if (context != NULL){
        context->title = "GCD";
        context->message = "GCD is amazing.";
        context->cancelButtonTitle = "OK";
        dispatch_async_f(mainQueue,
                         (void *)context,
                         displayAlertView);
        // 輸出當前執行緒
        NSLog(@"Current thread = %@", [NSThread currentThread]);
        // 輸出主執行緒
        NSLog(@"Main thread = %@", [NSThread mainThread]);
    }
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor = [UIColor whiteColor];

    self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

執行結果

主控台輸出:

2013-05-10 10:59:21.871 GCDExcuteUITest[1661:c07] Current thread = <NSThread: 0x7113d00>{name = (null), num = 1}
2013-05-10 10:59:21.872 GCDExcuteUITest[1661:c07] Main thread = <NSThread: 0x7113d00>{name = (null), num = 1}

可見無論是啟動時的當前執行緒還是主執行緒,指向的都是同一支主執行緒(num = 1),這正是 UI 操作能正確執行的原因。

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