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
相关文章推荐
- IOS-IAP 苹果支付
- iOS笔记之tableView
- 使用ReactiveCocoa实现iOS平台响应式编程
- iOS 无线滚动相册之scrollView
- iOS笔记之手势
- 【转】Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得
- IOS绘图
- IOS单元测试(1)
- FFmpeg在iOS上完美编译
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十七)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十七)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十七)
- 10步成为专业iOS开发者——从零起步
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十六)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十六)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十六)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十五)
- ios的一些开源资源
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十五)
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(十五)