Use GCD to perform UI operations in IOS development (60)

Source: Internet
Author: User

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}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.