UI_Block传值
2016-01-23 14:46
405 查看
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootViewController *rootVC = [[RootViewController alloc]init];
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:rootVC];
self.window.rootViewController = naVC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
#import "RootViewController.h"
#import "TwoViewController.h"
//typedef之后,该block的别名就是block的名称
typedef void(^argBlock)();
typedef void(^senderBlock)(NSString *str);
@interface RootViewController ()
@property(nonatomic,copy)argBlock circleBlock;
@end
@implementation RootViewController
//将block作为方法的参数,作为方法参数的时候,必须使用typedef修饰
-(void)testBlockWithBlock:(argBlock)senderBlock name:(NSString *)name{
NSLog(@"name---%@",name);
//当block作为参数的时候,和普通类型一样的,普通类型是需要我们使用该参数,block类型是需要我们调用该参数block
senderBlock();
}
//带参数的block作为一个方法的参数
-(void)myBlockWithBlock:(senderBlock)sender{
//将一个字符串传递出去
sender(@"我是要传递出去的字符串");
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Block学习";
//使用typedef的block
argBlock typeBlock = ^(){
NSLog(@"MLGBWC,我是typedef");
};
typeBlock();
//调用带block参数的方法
[self testBlockWithBlock:^{
NSLog(@"参数block");
}name:@"name"];
//__block外部串值 //block方法传递参数
__block NSString *Vstr = nil;
[self myBlockWithBlock:^(NSString *str) {
Vstr = str;
NSLog(@"str---%@",str);
}];
NSLog(@"Vstr---%@",Vstr);
//Block的学习
//第一种:
无返回值无参数的block定义以及实现
//void(^)(void)
当前block的类型
//block的名称为 oneBlock,类似于C语言中的函数名
//赋值号左边为block声明,右边为实现
void (^oneBlock)(void) = ^(){
NSLog(@"Hello World");
};
//调用block
不管有没有参数,都需要书写小括号
oneBlock();
//调用外部的block
//第二种:
有返回值无参数的block声明及实现
//有返回值的block需要在实现体内 return返回值
int (^twoBlock)(void) = ^(){
return 1000;
};
//调用block
int b = twoBlock();
NSLog(@"b---%d",b);
//第三种:
无返回值有参数的block声明及实现
void(^threeBlock)(NSString *name) = ^(NSString *bb){
NSLog(@"我是bb--%@",bb);
};
//调用block
threeBlock(@"aaaaaaa");
//第三种://两个参数的block
void(^fourBlock)(NSString *name,NSString *sex) = ^(NSString *cc,NSString *dd){
NSLog(@"%@,%@",cc,dd);
//拼接
NSString *pinjie = [cc stringByAppendingString:dd];
NSLog(@"%@",pinjie);
//拼接.2方法,一般拼接文件路径
NSString *pinjie2 = [cc stringByAppendingPathComponent:dd];
NSLog(@"%@",pinjie2);
};
//调用
fourBlock(@"Han",@"OBa");
//第四种:
有返回值有参数的block声明及实现
NSString *(^fiveBlock)(NSString *name) = ^(NSString *name){
return name;
};
//调用
NSString *name = fiveBlock(@"口鼻");
NSLog(@"%@",name);
//在block实现体内
使用外部变量
//当我们需要在block实现体内
对外部的局部变量做更改的时候,需要在局部变量的声明之前加双下划线block(__block).未添加__block修饰的局部变量,block并没有持有该变量,所以block只能使用该变量,但是不能做更改。使用__block修饰的局部变量,在block实现体
使用的时候,block是真正持有该变量,所以可以做修改
//__block修饰的局部变量,实际上是将该变量的作用域范围扩大了一些,不光能在ViewDidLoad方法中使用该变量,也可以在block函数的作用域中使用该变量
__block int k = 1000;
//声明并实现一个block
void (^myBlock)(void) = ^(){
NSLog(@"k-----%d",++k);
};
myBlock();
NSLog(@"%d",k);
//block的循环引用,有可能会造成内存泄露
//当block是self的一个属性的时候,我们在block实现体内需要使用到self得时候,有可能会造成循环引用,导致的结果为self的引用计数有可能为2,释放不掉,造成内存泄露。所以我们需要使用__weak关键字来修饰self对象,让该对象在block实现体内不需要内存管理,这个时候引用计数也就不会增加。
//在ARC下使用__weak,在MRC下使用__block修饰。
__weak RootViewController *my_self = self;
self.circleBlock = ^(){
my_self.navigationItem.title =@"AAA";
};
//右按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
}
/*
//在方法外部声明并实现block (了解知道就ok)
void (^outBlock)(void) = ^(){
NSLog(@"外面的block");
};
*/
//右按钮回调方法
-(void)rightAction:(UIBarButtonItem *)sender{
NSLog(@"跳转到下个界面");
TwoViewController *twoVC = [[TwoViewController alloc]init];
//调用two页面的block,接收block的参数值
twoVC.passSender = ^(NSString *str){
NSLog(@"title-----%@",str); //打印,用于查看是否将值传递过来了
self.navigationItem.title = str;
};
[self.navigationController pushViewController:twoVC animated:YES];
}
- (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
#import "TwoViewController.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"MLGBWC";
//左按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
self.navigationItem.leftBarButtonItem = leftButton;
}
//左按钮的回调方法
-(void)leftAction:(UIBarButtonItem *)sender{
NSLog(@"返回上一个界面");
//使用block将该界面的值传递到上个界面
self.passSender(@"标题");
[self.navigationController popViewControllerAnimated:YES];
}
- (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
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootViewController *rootVC = [[RootViewController alloc]init];
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:rootVC];
self.window.rootViewController = naVC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
@end
#import "RootViewController.h"
#import "TwoViewController.h"
//typedef之后,该block的别名就是block的名称
typedef void(^argBlock)();
typedef void(^senderBlock)(NSString *str);
@interface RootViewController ()
@property(nonatomic,copy)argBlock circleBlock;
@end
@implementation RootViewController
//将block作为方法的参数,作为方法参数的时候,必须使用typedef修饰
-(void)testBlockWithBlock:(argBlock)senderBlock name:(NSString *)name{
NSLog(@"name---%@",name);
//当block作为参数的时候,和普通类型一样的,普通类型是需要我们使用该参数,block类型是需要我们调用该参数block
senderBlock();
}
//带参数的block作为一个方法的参数
-(void)myBlockWithBlock:(senderBlock)sender{
//将一个字符串传递出去
sender(@"我是要传递出去的字符串");
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Block学习";
//使用typedef的block
argBlock typeBlock = ^(){
NSLog(@"MLGBWC,我是typedef");
};
typeBlock();
//调用带block参数的方法
[self testBlockWithBlock:^{
NSLog(@"参数block");
}name:@"name"];
//__block外部串值 //block方法传递参数
__block NSString *Vstr = nil;
[self myBlockWithBlock:^(NSString *str) {
Vstr = str;
NSLog(@"str---%@",str);
}];
NSLog(@"Vstr---%@",Vstr);
//Block的学习
//第一种:
无返回值无参数的block定义以及实现
//void(^)(void)
当前block的类型
//block的名称为 oneBlock,类似于C语言中的函数名
//赋值号左边为block声明,右边为实现
void (^oneBlock)(void) = ^(){
NSLog(@"Hello World");
};
//调用block
不管有没有参数,都需要书写小括号
oneBlock();
//调用外部的block
//第二种:
有返回值无参数的block声明及实现
//有返回值的block需要在实现体内 return返回值
int (^twoBlock)(void) = ^(){
return 1000;
};
//调用block
int b = twoBlock();
NSLog(@"b---%d",b);
//第三种:
无返回值有参数的block声明及实现
void(^threeBlock)(NSString *name) = ^(NSString *bb){
NSLog(@"我是bb--%@",bb);
};
//调用block
threeBlock(@"aaaaaaa");
//第三种://两个参数的block
void(^fourBlock)(NSString *name,NSString *sex) = ^(NSString *cc,NSString *dd){
NSLog(@"%@,%@",cc,dd);
//拼接
NSString *pinjie = [cc stringByAppendingString:dd];
NSLog(@"%@",pinjie);
//拼接.2方法,一般拼接文件路径
NSString *pinjie2 = [cc stringByAppendingPathComponent:dd];
NSLog(@"%@",pinjie2);
};
//调用
fourBlock(@"Han",@"OBa");
//第四种:
有返回值有参数的block声明及实现
NSString *(^fiveBlock)(NSString *name) = ^(NSString *name){
return name;
};
//调用
NSString *name = fiveBlock(@"口鼻");
NSLog(@"%@",name);
//在block实现体内
使用外部变量
//当我们需要在block实现体内
对外部的局部变量做更改的时候,需要在局部变量的声明之前加双下划线block(__block).未添加__block修饰的局部变量,block并没有持有该变量,所以block只能使用该变量,但是不能做更改。使用__block修饰的局部变量,在block实现体
使用的时候,block是真正持有该变量,所以可以做修改
//__block修饰的局部变量,实际上是将该变量的作用域范围扩大了一些,不光能在ViewDidLoad方法中使用该变量,也可以在block函数的作用域中使用该变量
__block int k = 1000;
//声明并实现一个block
void (^myBlock)(void) = ^(){
NSLog(@"k-----%d",++k);
};
myBlock();
NSLog(@"%d",k);
//block的循环引用,有可能会造成内存泄露
//当block是self的一个属性的时候,我们在block实现体内需要使用到self得时候,有可能会造成循环引用,导致的结果为self的引用计数有可能为2,释放不掉,造成内存泄露。所以我们需要使用__weak关键字来修饰self对象,让该对象在block实现体内不需要内存管理,这个时候引用计数也就不会增加。
//在ARC下使用__weak,在MRC下使用__block修饰。
__weak RootViewController *my_self = self;
self.circleBlock = ^(){
my_self.navigationItem.title =@"AAA";
};
//右按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
}
/*
//在方法外部声明并实现block (了解知道就ok)
void (^outBlock)(void) = ^(){
NSLog(@"外面的block");
};
*/
//右按钮回调方法
-(void)rightAction:(UIBarButtonItem *)sender{
NSLog(@"跳转到下个界面");
TwoViewController *twoVC = [[TwoViewController alloc]init];
//调用two页面的block,接收block的参数值
twoVC.passSender = ^(NSString *str){
NSLog(@"title-----%@",str); //打印,用于查看是否将值传递过来了
self.navigationItem.title = str;
};
[self.navigationController pushViewController:twoVC animated:YES];
}
- (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
#import "TwoViewController.h"
@interface TwoViewController ()
@end
@implementation TwoViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"MLGBWC";
//左按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
self.navigationItem.leftBarButtonItem = leftButton;
}
//左按钮的回调方法
-(void)leftAction:(UIBarButtonItem *)sender{
NSLog(@"返回上一个界面");
//使用block将该界面的值传递到上个界面
self.passSender(@"标题");
[self.navigationController popViewControllerAnimated:YES];
}
- (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
相关文章推荐
- UI_视图导航器
- UI_滚动视图
- UI_滑竿,分段控制器
- UI_手势
- UI_炫彩方框,旋转跑马灯,炫彩画板
- UI_Button的应用
- UI_封装
- Leetcode 63. Unique Paths II
- KeyValueCoding
- 优化UITableViewCell高度计算的那些事(RunLoop)
- LeetCode63. Unique Paths II
- Leetcode 62. Unique Paths
- ios学习之模仿韩寒"one.一个"UI
- iOS_UItableVeiwCell 点击取消高亮状态
- Cannot launch AVD in emulator on Windows: ERROR: x86 emulation currently requires hardware accelerat
- iOS UIView初识
- Android UI性能优化详解
- UI控件
- 231,NSOperation和NSOperationQueue
- [iOS]把16进制(#871f78)颜色转换UIColor