你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS開發之使用GCD執行UI操作

IOS開發之使用GCD執行UI操作

編輯:關於IOS

1 前言
本章作為入門,我們將介紹介紹一下,能使用GCD執行UI操作。

2 代碼實例
ZYViewController.h

 
#import <UIKit/UIKit.h>

@interface ZYViewController : UIViewController

@end

#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];
});
}

- (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

#import <UIKit/UIKit.h>

@class ZYViewController;

@interface ZYAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ZYViewController *viewController;

@end
ZYAppDelegate.m

view plaincopyprint?//聲明結構體AlertViewData

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];

// Override point for customization after application launch.
self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}

//聲明結構體AlertViewData
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];

// Override point for customization after application launch.
self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
運行結果

IOS開發之使用GCD執行UI操作
控制台結果

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}

 

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