码迷,mamicode.com
首页 > 其他好文 > 详细

NSOperation

时间:2015-04-01 11:04:04      阅读:92      评论:0      收藏:0      [点我收藏+]

标签:

前面了解了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

 

NSOperation

标签:

原文地址:http://www.cnblogs.com/cuiyw/p/4383045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!