码迷,mamicode.com
首页 > 移动开发 > 详细

IOS之block

时间:2015-04-07 19:33:33      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

IOS之block

1.block

  回调(按钮的处理事件,网络下载后的回调处理)

  (1)按钮target-action  方法传入按钮中

  (2)表格视图   传入self指针

  (3)block  语句块,解决回调

 2.block语句

    //1.block变量的定义
    //技巧:语法诡异    
    //定义block  -->类似 void (*func)(); 函数指针
    void (^block)();    
    //定义语句块  ()
    block = ^void (){   //定义block语句块-->存到block变量中
     
        NSLog(@"I am a block");
    };
    block();   //执行
    
    //2. eg:计算两数之和   int myAdd(int a,int b);
    int (^myAdd) (int a,int b) = ^int(int a,int b){
    
        return a+b;
    };
    int sum = myAdd(10,30);
    NSLog(@"sum = %d",sum);
    
    //3.block捕获外部变量  --> 不能修改局部变量的值 ,可以使用和修改实例变量
    // __block int a;(外部变量)
//    int num = 10;
    __block int value = 100;
    void (^b1)() = ^void(){
    
        _page = 1;
        value++;
//        num++;    //error        
        //可能有警告,因内存问题,注:
        //  __weak typeof(self) weakSelf = self;   //block外定义
        //  weakSelf.url = @"text";
        self.url = @"text";   //相互引用造成的内存问题
    };
    
    b1();

 3.block应用

  (1)NSMutableArray排序

#import "Dog.h"
//oc应用
    //1.NSMutableArray排序
    Dog *ahua = [[Dog alloc] init];
    ahua.nickName = @"ahua";
    ahua.age = 4;
    
    Dog *amiao = [[Dog alloc] init];
    amiao.nickName = @"amiao";
    amiao.age = 3;

    Dog *dahuang = [[Dog alloc] init];
    dahuang.nickName = @"dahuang";
    dahuang.age = 5;

    NSMutableArray *muarr = [[NSMutableArray alloc] initWithArray:@[ahua,amiao,dahuang]];
    [muarr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
     
        Dog *adog = obj1;
        Dog *bdog = obj2;
        //return adog.age > bdog.age;
        return [adog.nickName compare:bdog.nickName];   //按nickName排
    }];
    
    for(Dog *dog in muarr){
    
        NSLog(@"name = %@,age = %d",dog.nickName,dog.age);
    }

  (2)UIView动画

 //2.UIView动画
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    label.text = @"I am a label";
    label.backgroundColor = [UIColor redColor];
    [self.view addSubview:label];
    
    [UIView animateWithDuration:2 animations:^{
        CGRect rect = label.frame;
        rect.origin.y += 200;
        label.frame = rect;
    } completion:^(BOOL finished) {   //可嵌套
        NSLog(@"结束动画");
       
        [UIView animateWithDuration:2 animations:^{
            label.transform = CGAffineTransformMakeRotation(M_PI);   //旋转180
        } completion:^(BOOL finished) {
            
        }];
    }];

  (3)反向传值

#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
//void action(NSString *color);
//void (^action)(NSString *color);
-(void)setChangeBackgroundColor:(void(^)(NSString *color))action;
@end
#pragma mark -
-(void)setChangeBackgroundColor:(void (^)(NSString *))action{
    
    _action = action;
}

#pragma mark -
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //反向传值
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(100, 200, 100, 30);    //根据实际调
    [btn setTitle:@"返回" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}
-(void)btnClick:(UIButton *)btn{
    
    if(_action){
    
        _action(@"blue");  //逆向传值
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

   在第一个ViewConreoller接收

-(void)btnClick:(UIButton *)btn{

    SecondViewController *svc = [[SecondViewController alloc] init];
        svc.view.backgroundColor = [UIColor yellowColor];
    [svc setChangeBackgroundColor:^(NSString *color) {
        if([color isEqualToString:@"blue"]){
        
            self.view.backgroundColor = [UIColor blueColor];
        }
    }];
    
    [self presentViewController:svc animated:YES completion:nil];
}

 

IOS之block

标签:

原文地址:http://www.cnblogs.com/wlrBlogs/p/4397579.html

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