iOS Block的使用
2015-11-11 16:52
417 查看
直接上代码:
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface RootViewController :
UIViewController
@end
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// 无参数
无返回值的block 打印一条语句
void(^myBlock)() = ^void{
NSLog(@"11");
};
myBlock();
// 有参数
有返回值 两个整数相加
int (^sum) (int,int) = ^(int a,
int b){
return a + b;
};
int s = sum(1,2);
NSLog(@"%d",s);
UIButton *button = [UIButton
buttonWithType:UIButtonTypeCustom];
button.frame =
CGRectMake(20,
100, 100,
50);
[button setTitle:@"点我呀"
forState:UIControlStateNormal];
button.backgroundColor = [UIColor
magentaColor];
[button addTarget:self
action:@selector(jumpTo:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview: button];
}
- (void)jumpTo:(UIButton *)button
{
SecondViewController *second = [[SecondViewController
alloc] init];
//接收Block回调的值
second.sendMessage = ^(NSString *name,NSString *address){
//()里面的两个参数
就是传回来的值
NSLog(@"%@",name);
//修改按钮上的文字
//会造成引用计数加1
__block
UIButton *tempButton = button;//避免造成引用计数加1的方式
[tempButton setTitle:name
forState:UIControlStateNormal];
};
[self.navigationController
pushViewController:second
animated:YES];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//***********************************************************************//
#import <UIKit/UIKit.h>
@interface SecondViewController :
UIViewController
#pragma mark --Block属性 copy修饰
(block本身会在栈区
用copy是它存在堆区
安全操作)
@property(nonatomic,copy)void(^sendMessage)(NSString
*name,NSString *address);
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super
viewDidLoad];
self.view.backgroundColor = [UIColor
whiteColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem
alloc] initWithTitle:@"返回"
style:UIBarButtonItemStylePlain
target:self
action:@selector(backTo)];
}
- (void)backTo
{
self.sendMessage(@"尼玛傻B二百五",@"屌丝");
[self.navigationController
popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface RootViewController :
UIViewController
@end
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// 无参数
无返回值的block 打印一条语句
void(^myBlock)() = ^void{
NSLog(@"11");
};
myBlock();
// 有参数
有返回值 两个整数相加
int (^sum) (int,int) = ^(int a,
int b){
return a + b;
};
int s = sum(1,2);
NSLog(@"%d",s);
UIButton *button = [UIButton
buttonWithType:UIButtonTypeCustom];
button.frame =
CGRectMake(20,
100, 100,
50);
[button setTitle:@"点我呀"
forState:UIControlStateNormal];
button.backgroundColor = [UIColor
magentaColor];
[button addTarget:self
action:@selector(jumpTo:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview: button];
}
- (void)jumpTo:(UIButton *)button
{
SecondViewController *second = [[SecondViewController
alloc] init];
//接收Block回调的值
second.sendMessage = ^(NSString *name,NSString *address){
//()里面的两个参数
就是传回来的值
NSLog(@"%@",name);
//修改按钮上的文字
//会造成引用计数加1
__block
UIButton *tempButton = button;//避免造成引用计数加1的方式
[tempButton setTitle:name
forState:UIControlStateNormal];
};
[self.navigationController
pushViewController:second
animated:YES];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//***********************************************************************//
#import <UIKit/UIKit.h>
@interface SecondViewController :
UIViewController
#pragma mark --Block属性 copy修饰
(block本身会在栈区
用copy是它存在堆区
安全操作)
@property(nonatomic,copy)void(^sendMessage)(NSString
*name,NSString *address);
@end
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super
viewDidLoad];
self.view.backgroundColor = [UIColor
whiteColor];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem
alloc] initWithTitle:@"返回"
style:UIBarButtonItemStylePlain
target:self
action:@selector(backTo)];
}
- (void)backTo
{
self.sendMessage(@"尼玛傻B二百五",@"屌丝");
[self.navigationController
popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- iOS常用第三方库
- iOS多线程
- iOS读写本地文件
- IOS短信验证码倒计时添加
- IOS消息推送之APNS
- IOS7 点击空白处隐藏键盘的几种方法
- iOS数据持久化
- iOS沙盒目录,Documents、Library、tmp
- [IOS]edgesForExtendedLayout、automaticallyAdjustsScrollViewInsets
- iOS开发多线程篇—线程安全
- iOS开发的22个奇葩技巧
- 用Ranorex进行安卓IOS自动化测试
- iOS沙盒机制
- [iOS基础]关于Mail的一切
- iOS开发多线程篇—创建线程
- iOS加密之MD5加密
- iOS的主要框架介绍
- iOS 心得十 按钮添加长按手势
- iOS 域名解析ip(解析)
- ios开发是更加“省时”的编程方式