标签:
前面了解了GCD也比较详细,所以Thread和NSOperation大概了解一下。参考:http://blog.csdn.net/crycheng/article/details/21799611
还是上代码
// // ViewController.m // Operation // // Created by City--Online on 15/4/1. // Copyright (c) 2015年 City--Online. All rights reserved. // #import "ViewController.h" @interface ViewController () { UIButton *btn; NSBlockOperation *blockoperation; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; btn=[UIButton buttonWithType:UIButtonTypeCustom]; [btn setTitle:@"按钮" forState:UIControlStateNormal]; btn.backgroundColor=[UIColor redColor]; btn.frame=CGRectMake(30, 30, 50, 50); [btn addTarget:self action:@selector(btnclick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; NSInvocationOperation *operation=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(threadfun) object:nil]; NSOperationQueue *operationqueue=[[NSOperationQueue alloc]init]; [operationqueue addOperation:operation]; blockoperation=[NSBlockOperation blockOperationWithBlock:^{ NSLog(@"blockoperation"); }]; } -(void)btnclick:(id)sender { //增加block到线程中 [blockoperation addExecutionBlock:^{ NSLog(@"addExecutionBlock"); }]; [blockoperation setCompletionBlock:^{ NSLog(@"setCompletionBlock"); }]; [blockoperation start];//不能放在addExecutionBlock之前start } -(void)threadfun { NSLog(@"threadfun"); //更新主线程 [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO]; } -(void)updateUI { NSLog(@"uodateUI"); [btn setTitle:@"123" forState:UIControlStateNormal]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
标签:
原文地址:http://www.cnblogs.com/cuiyw/p/4383045.html