iOS中使用Block反响传值的用法
2015-11-14 10:51
531 查看
方向传值,用block传值要比用代理传值简单多,并且代码简单。
有一个A页面和B页面,B页面的值传给A页面,这就是用Block方向传值。
下面直接上代码
A页面代码
B页面代码如下:
在.h文件里申明一个Block
这样即可完成页面反响传值。。。。。。
有一个A页面和B页面,B页面的值传给A页面,这就是用Block方向传值。
下面直接上代码
A页面代码
UIButton * playButton = [UIButton buttonWithType:UIButtonTypeCustom]; playButton.frame = CGRectMake(100, 220, 100, 80); [playButton setTitle:@"选择播放视频" forState:UIControlStateNormal]; [playButton setBackgroundColor:[UIColor grayColor]]; [playButton addTarget:self action:@selector(playVideoGo) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:playButton]; self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 320, 100, 30)]; self.textLabel.backgroundColor = [UIColor greenColor]; [self.textLabel setText:@"回调传值"]; [self.view addSubview:self.textLabel];
- (void)playVideoGo { playVideoViewController * vc = [[playVideoViewController alloc] init]; vc.returnBlock = ^(NSString * showText){ NSLog(@"----------%@",showText); self.textLabel.text = showText; }; [self presentViewController:vc animated:YES completion:nil]; //[self.navigationController pushViewController:vc animated:YES]; }
B页面代码如下:
在.h文件里申明一个Block
typedef void (^ReturnTextBlock)(NSString * showText);在.m文件中
self.inputFiled = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 30)]; self.inputFiled.borderStyle = UITextBorderStyleRoundedRect; [self.view addSubview:self.inputFiled]; UIButton * playButton = [UIButton buttonWithType:UIButtonTypeCustom]; playButton.frame = CGRectMake(100, 220, 100, 80); [playButton setTitle:@"回调" forState:UIControlStateNormal]; [playButton setBackgroundColor:[UIColor grayColor]]; [playButton addTarget:self action:@selector(playVideoBack) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:playButton];
-(void)playVideoBack { self.returnBlock(self.inputFiled.text); [self dismissViewControllerAnimated:YES completion:nil]; //[self.navigationController popViewControllerAnimated:YES]; }
这样即可完成页面反响传值。。。。。。
相关文章推荐
- iOS9新内容适配
- 我的iOS学习历程 - TargetAction
- 我的iOS学习历程 - TargetAction
- Xcode各版本官方下载及百度云盘下载, Mac和IOS及Xcode版本历史.
- Why BIOS loads MBR into 0x7C00 in x86 ?
- iOS缓存机制
- iOS开发规范
- iOS9.2 PCH文件的创建和使用
- ios学习笔记(四)定时器
- ios学习笔记(四)定时器
- iOS开发——学习runtime的理解和心得
- iOS开发——通过代码方式使用AutoLayout
- IOS流媒体播放器V1.1_rtsp、rmtp、m3u8、mms、mmsh
- IOS流媒体播放器V1.1_rtsp、rmtp、m3u8、mms、mmsh
- 三种实现iOS瀑布流的思路总结
- iOS巅峰之SBJSON解析
- IOS中实现图片点击全屏预览
- [iOS] 建立与使用Framework
- ios快捷键
- 初学IOS开发记录