Block传值 反向传值
来源:互联网 发布:怎么改淘宝店铺名称 编辑:程序博客网 时间:2024/05/21 14:42
反向传值可以用单例、代理和Block,当然SEL也可以,下面简单介绍一下Block传值,直接上代码
1.新建一个single view工程
2.在AppDelegate.m中
self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; self.viewController.title = @"Block传值"; UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self.viewController]; self.window.rootViewController = nc;
3.ViewController.h
@interface ViewController : UIViewController { UILabel *valueLabel;//用来接收SecondViewController传回的值}@end
4.实现ViewController.m
- (void)viewDidLoad{ [super viewDidLoad]; valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 50)]; [self.view addSubview:valueLabel]; [valueLabel release]; UIBarButtonItem *nextBtn = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(nextClick)]; self.navigationItem.rightBarButtonItem = nextBtn; [nextBtn release];}
- (void)nextClick{ SecondViewController *svc = [[SecondViewController alloc] init]; svc.backValue = ^(NSString *strValue) {//设置SecondViewController里边的block属性,这是本程序的关键 valueLabel.text = strValue; }; [self.navigationController pushViewController:svc animated:YES]; [svc release];}
5.SecondViewController.h
@interface SecondViewController : UIViewController@property (nonatomic, copy) void (^backValue)(NSString *strValue);@property (nonatomic, retain) UITextField *text;@end
6.实现SecondViewController.m
@implementation SecondViewController@synthesize backValue;@synthesize text;- (void)viewDidLoad{ [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; text = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 40)]; text.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:text]; [text release]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame = CGRectMake(10, 110, 60, 30); [btn setTitle:@"返回" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn];}- (void)btnClick{ self.backValue(self.text.text);//调用block方法 [self.navigationController popViewControllerAnimated:YES];}@end
效果图
返回之后:
0 0
- Block传值 反向传值
- iOS---block反向传值
- iOS block反向传值
- Block 反向传值的简单应用
- iOS block/闭包回调( 反向传值 )
- iOS开发 UI 正向传值 反向传值,Block传值
- ios中控制器之间的反向传值之---Block
- Block在UITableViewcell中的正向和反向传值
- iOS --- block的一些使用方法(反向传值)
- 浅析 协议和block的反向传值
- IOS学习之——Block反向传值
- swift中delegate与block的反向传值
- IOS 多种反向传值对比(代理,block,通知)
- 5、iOS传值的方法-Block反向传值
- 使用block闭包是实现两个页面间的反向传值
- iOS 用block反向传值-视频列表进入详情后返回刷新数据
- Swift之delegate代理和Block闭包的反向传值
- iOS 工作之余小总结之(六)(4) --四种界面反向传值的方式:block反向传值
- android uses-permission 配置详解
- Codeforces Round #222 (Div. 2)
- extern
- HTML5实战与剖析之HTMLDocument变化(readyreState属性、兼容模式和head属性)
- jQuery API .after()
- Block传值 反向传值
- android CTS Test
- MyEclipse快捷键大全
- 条款2:最好使用C++转型操作符
- 解决"应用程序无法启动,因为应用程序的并行配置不正确"问题
- davinci平台的spi驱动框架
- 博客地址链接 -- 学习自用
- 每天学点English Email(商务性结尾2)
- MySQL数据移植到Oracle中碰到的异常总结一