iOS 笔记之block

#import "RootViewController.h"
#import "SecondViewController.h"

@interface RootViewController ()<UIActionSheetDelegate>
@property(nonatomic, retain)UIButton *button;
@property(nonatomic, retain)UIActionSheet *sheet;
@end

@implementation RootViewController

int maxValue(int a, int b) {
    return a > b ? a : b;
}


- (void)dealloc {
    [_sheet release];
    [_button release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor orangeColor];
//    self.sheet = [[UIActionSheet alloc] initWithTitle:@"你愁啥" delegate:self cancelButtonTitle:@"抽你咋得" destructiveButtonTitle:@"再丑一个试试" otherButtonTitles:@"试试就试试",@"谁怕谁啊", nil];
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.frame = CGRectMake(100, 100, 150, 50);
//    button.layer.borderWidth = 1;
//    [self.view addSubview:button];
//    [button setTitle:@"googol" forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(100, 100, 150, 50);
    [self.view addSubview:self.button];
    self.button.layer.borderWidth = 1;
    [self.button setTitle:@"下一页" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
    NSLog(@"%p", maxValue);

     函数指针
    
     函数指针是保存函数地址的指针, 函数指针的类型先把函数名字拿掉, 然后写一个小括号, 这个括号不能省略, 然后里面写*和变量名, 最后把形参名拿掉
//    int (*p)(int, int) = maxValue;
//    // 通过函数指针进行函数调用
//    NSLog(@"%d", p(4, 6));

}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        NSLog(@"zaichouyigeshishi");
    } else if (buttonIndex == 1) {
        NSLog(@"shishijiushishis");
    } else if (buttonIndex == 2) {
        NSLog(@"shuipashuia");
    } else {
        NSLog(@"gun");
    }
}


- (void)click:(UIButton *)button {

    [self.sheet showInView:self.view];





    // 等号左边: 先写返回值类型, 然后(), 里面写^和block的名字, 然后在()里面写参数类型, 把函数指针的*p 变成block
    // 等号右边: 先以^开头, 然后写形式参数列表, 列表里地形参和等号左边的形参类型和顺序相应, 最后写函数部分
    // 注意有返回值的block一定要写return, 在最后以分号结尾
//    int (^block)(int, int) = ^(int a, int b) {
//        // 函数的功能在大括号里面写
//        return 10;
//    };
//    NSLog(@"%d", block(12, 9));


    // block 不调用就不会被执行
    // 没有参数, 没有返回值
//    void (^block)() = ^() {
//        NSLog(@"// 函数的功能在大括号里面写");
//    };
//    block();


    // block的类型
    // 等号左边去掉block的名剩下的就是block的类型
//    void (^block)(UIColor *) = ^(UIColor *color) {
//        self.view.backgroundColor = color;
//    };
//    block([UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1]);

    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];

//    // 1. 先写一个block, 没有参数, 没有返回值, 改变背景颜色
//    void (^block)() = ^ {
//        self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
//    };
//    // 2. 传值, 这次传一个block
//    secondVC.block = block;


    // 没有返回值, 参数是字符串
    void (^block)(NSString *) = ^(NSString *str) {
        NSLog(@"%@", str);
        // 对数据的处理, 操作, 都在block里面写
    };
    secondVC.block = block;

//     block 如果不进行拷贝的话, 可能会在栈区或者全局区, 这两个不能对他进行内存上的控制, 所有不安全, 所有一个用copy, 就可以把block拷贝一份到堆区



}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end






自定义block:
#import <UIKit/UIKit.h>
typedef void (^Block)(NSString *);
@interface SecondViewController : UIViewController
// 写一条属性, 用来接收前一页的block
//@property(nonatomic, copy)void(^block)();
@property(nonatomic, copy)Block block;
@end

#import "SecondViewController.h"

@interface SecondViewController ()
@property(nonatomic, retain)UIButton *button;
@end

@implementation SecondViewController

- (void)dealloc {
    // block 作为属性释放用它自己提供的方法
    Block_release(_block);
    [_button release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(100, 100, 150, 50);
    [self.view addSubview:self.button];
    self.button.layer.borderWidth = 1;
    [self.button setTitle:@"返回" forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}

- (void)click:(UIButton *)button {
    [self.navigationController popToRootViewControllerAnimated:YES];
    // 3. 调用block
    self.block(@"我是觉得解放路附近");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值