1 Preface
This chapter introduces how to use GCD to perform UI operations.
2. code example
ZYViewController. h
[Plain]
# Import <UIKit/UIKit. h>
@ Interface ZYViewController: UIViewController
@ End
# Import <UIKit/UIKit. h>
@ Interface ZYViewController: UIViewController
@ End
ZYViewController. m
[Plain]
(Void) viewDidLoad
{
[Super viewDidLoad];
// Dispatch_queue_t: a scheduling queue is a lightweight object, and subsequent execution of your application submission module. Obtain the main queue Column
Dispatch_queue_t mainQueue = dispatch_get_main_queue ();
/*
Submit a block for asynchronous execution in a scheduling queue and return immediately.
Queue
Queue to submit blocks. The queue is retained by the system until the block has been run to complete. This parameter cannot be blank.
Block
The block is submitted to the target scheduling queue. This function executes block replication and releases callers. This parameter cannot be blank.
*/
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: a scheduling queue is a lightweight object, and subsequent execution of your application submission module. Obtain the main queue Column
Dispatch_queue_t mainQueue = dispatch_get_main_queue ();
/*
Submit a block for asynchronous execution in a scheduling queue and return immediately.
Queue
Queue to submit blocks. The queue is retained by the system until the block has been run to complete. This parameter cannot be blank.
Block
The block is submitted to the target scheduling queue. This function executes block replication and releases callers. This parameter cannot be blank.
*/
Dispatch_async (mainQueue, ^ (void)
{
[[UIAlertView alloc] initWithTitle: @ "GCD"
Message: @ "GCD is amazing! "
Delegate: nil cancelButtonTitle: @ "OK"
OtherButtonTitles: nil, nil] show];
});
}
ZYAppDelegate. h
[Plain]
# 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
[Plain] view plaincopyprint? // Declare the struct AlertViewData
Typedef struct {
Char * title;
Char * message;
Char * cancelButtonTitle;
} AlertViewData;
@ Implementation ZYAppDelegate
// C function
Void displayAlertView (void * paramContext ){
AlertViewData * alertData = (AlertViewData *) paramContext; NSString * title =
[NSString stringwithuf8string: alertData-> title];
NSString * message =
[NSString stringwithuf8string: alertData-> message];
NSString * cancelButtonTitle =
[NSString stringwithuf8string: 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 didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
// Obtain the main thread queue
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 );
// Output the current thread
NSLog (@ "Current thread = % @", [NSThread currentThread]);
// Output the main thread
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;
}
// Declare the struct AlertViewData
Typedef struct {
Char * title;
Char * message;
Char * cancelButtonTitle;
} AlertViewData;
@ Implementation ZYAppDelegate
// C function
Void displayAlertView (void * paramContext ){
AlertViewData * alertData = (AlertViewData *) paramContext; NSString * title =
[NSString stringwithuf8string: alertData-> title];
NSString * message =
[NSString stringwithuf8string: alertData-> message];
NSString * cancelButtonTitle =
[NSString stringwithuf8string: 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 didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
// Obtain the main thread queue
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 );
// Output the current thread
NSLog (@ "Current thread = % @", [NSThread currentThread]);
// Output the main thread
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;
}
Running result
Console result
10:59:21. 871 GCDExcuteUITest [1661: c07] Current thread = <NSThread: 0x7113d00> {name = (null), num = 1}
10:59:21. 872 GCDExcuteUITest [1661: c07] Main thread = <NSThread: 0x7113d00> {name = (null), num = 1}