iOS Block
来源:互联网 发布:c#开发书籍推荐 知乎 编辑:程序博客网 时间:2024/05/04 23:11
//在需要传值的.h文件中声明block属性
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (nonatomic, retain)UIButton *button;
#warning block第一步 声明block属性
//类型一定为copy
@property (nonatomic, copy)void(^myBlock)(UIColor *color);
@end
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
#warning block第四步 属性block内存释放
- (void)dealloc
{
Block_release(_myBlock);
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(20, 100, 100, 40);
self.button.backgroundColor = [UIColor greenColor];
[self.button setTitle:@"第一页" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
- (void)buttonAction:(UIButton *)button
{
[self.navigationController popToRootViewControllerAnimated:YES];
#warning block第二步 执行block
self.myBlock([UIColor blackColor]);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//在被传值的.h页面自定义一个按钮,点击按钮完成传值
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface RootViewController : UIViewController
@property (nonatomic, retain)UIButton *button;
@end
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(20, 100, 100, 40);
self.button.backgroundColor = [UIColor greenColor];
[self.button setTitle:@"根" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
}
- (void)buttonAction:(UIButton *)button
{
FirstViewController *first = [[FirstViewController alloc]init];
[self.navigationController pushViewController:first animated:YES];
//无参数 无返回值
//block修饰 外部变量int a;
__block int a = 0;
void(^block1)(void) = ^(void)
{
a++;
NSLog(@"block1 = %d", a);
};
//调用方法
block1();
//有参数,无返回值
void(^block2)(int a, NSString *string) = ^(int a, NSString *string)
{
NSLog(@"string = %@", string);
};
block2(10, @"大水杯");
//无参数,有返回值
NSString *(^block3)(void) = ^(void)
{
return @"登山包";
};
NSLog(@"block3 = %@", block3());
//有参数,有返回值
NSString *(^block4)(int a, NSString *string) = ^(int a, NSString *string)
{
return[string stringByAppendingString:@"大水杯"];
};
NSString *str = block4(11, @"124");
NSLog(@"%@", str);
#pragma mark ---重点1.block内存管理
//当block内部使用外部变量时,系统会将内存移到栈区 Stack:栈区
//系统分配的内存都放在栈区
//自己开辟的内存在堆区
NSLog(@"block1 = %@", block1);
//block默认放在全局区 Global:全局区
NSLog(@"block2 = %@", block2);
#pragma mark---解决循环引用的方法 将self定义为block变量
__block RootViewController *rootV = self;
#warning block第三步 block实现
first.myBlock = ^(UIColor *color)
{
// 记住 回调回来的参数 直接用,他是另一个页面传过来的
#pragma mark ------重要 如果在block内部中使用self属性,会将self引用计数加一,导致循环引用,因此不能使用self,而是将self定义为block变量
rootV.view.backgroundColor = color;
};
//当将block设置成copy类型的属性时, block会被放入堆区中,需要自己手动释放内存
//release时 block在堆区
NSLog(@"myBlock = %@", first.myBlock);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
- ios block
- IOS BLOCK
- ios block
- ios block
- ios block
- IOS BLOCK
- ios block
- iOS Block
- ios block
- IOS block
- iOS Block
- IOS block
- iOS BLOCK
- iOS Block
- IOS Block
- IOS block
- IOS Block
- IOS -BLOCK
- Unexpected UTF-8 BOM (decode using utf-8-sig)
- SEO实战:优质用户体验帮你突袭前三排名位
- /etc/profile文件详解
- MDK建立STM32F103工程步骤
- iOS UIDatePicker NSDateFormatter
- iOS Block
- 生活中的 真、善、美
- iOS隐藏状态栏的两种方法
- Help, I'm stuck in the event loop
- RabbitMQ与Redis队列对比
- tomcat内存监控笔记
- Android 系统构架
- 查看oracle系统的字符集
- 自定义ActionBar盘点---------NotBoringActionBar