您的位置:首页 > 移动开发 > IOS开发

iOS 笔记之block

2015-11-11 11:15 381 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: