iOS block在两个页面间的简单传值
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
#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]];
self.window.backgroundColor = [UIColor whiteColor];
// 给根控制器添加导航栏
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
self.window.rootViewController = navigationController; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h"
#import "BViewController.h"
@interface RootViewController ()
{
BViewController *bVC;
}
@end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 初始化button
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(, , , );
btn.backgroundColor = [UIColor greenColor];
[btn setTitle: @"PUSH到下一个控制器" forState:];
[btn setTitleColor:[UIColor orangeColor] forState:];
[btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
//初始化label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(, , , )];
label.backgroundColor = [UIColor redColor];
label.textColor = [UIColor yellowColor];
[self.view addSubview:label];
//初始化BViewController
bVC = [[BViewController alloc] init]; // 声明block(注意:这里只是声明,并没有执行block的代码,只有当block被调用才执行下面的代码)
bVC.titleString = ^(NSString *title){
label.text = title;
};
} - (void)btnAction:(UIButton *)sender{
// push到下一个控制器
[self.navigationController pushViewController:bVC animated:YES];
} @end
#import <UIKit/UIKit.h> typedef void(^titleBlock)(NSString *showText); @interface BViewController : UIViewController
// 注意:copy 把block从栈区推到堆区
@property (nonatomic, copy) titleBlock titleString; @end
#import "BViewController.h" @interface BViewController ()
{
UITextField *tf;
}
@end @implementation BViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
// 初始化tf
tf = [[UITextField alloc] initWithFrame:CGRectMake(, , , )];
tf.backgroundColor = [UIColor lightGrayColor];
tf.text = @"把值传到根控制器";
tf.textColor = [UIColor yellowColor];
[self.view addSubview:tf];
}
/**
* 当页面将要消失时,传值到根控制器
*/
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
if (tf.text.length != ) {
//执行block的代码(当执行此语句,就会跳到RootViewController的block声明的地方,执行block的代码)
self.titleString(tf.text);
}
} @end
iOS block在两个页面间的简单传值的更多相关文章
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...
- 转:在两个页面间翻转设置Animation动作的一些总结
今天碰到两个页面之间翻转的动作设计问题,发现了一些问题,故做个总结,很多都写在注释部分: 1.首先,我们来手动创建两个view以及相应的viewController.是手动,不是用IB (1)刚开始只 ...
- 使用block实现两个页面之间的传统价值观
第二个view声明一个block属性: @property (nonatomic, copy) void(^doTransferMsg)(NSString *_msg); 然后传值方法里检查block ...
- 【vue】两个页面间传参 - props
目录 Step1 设置可以 props 传递数据 Step2 跳转前页面中传递数据 Step3 跳转后的页面接收数据 从 A 页面跳转到 B 页面, 参数/数据通过 props 传递到 B 页面,这种 ...
- NavigationViewController页面间通信及传值
使用进行页面跳转时,应该使用方法来跳转至下一页面,这样的话,下一页面同样在容器中. 1AloneSetPrizeViewController *setPrize = [[AloneSetPrizeVi ...
- 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错
原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...
- iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...
- iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...
随机推荐
- SVN配置管理(trunk、branches、tags)
利用SVN的分支,合理地管理项目代码 由于SVN固有的特点,目录在SVN中并没有特别的意义,但是这三个目录却在大多数开源项目中存在,这是因为这三个目录反映了软件开发的通常模式. trunk是主分支,是 ...
- PHP 设计模式 笔记与总结(9)数据对象映射模式
[数据对象映射模式] 是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作.例如在代码中 new 一个对象,使用数据对象映射模式就可以将对象的一些操作比如设置一些属性,就会自动保存到数 ...
- 综合支撑【恶灵附身 Psycho Break】的世界观的概念艺术
综合支撑[恶灵附身 Psycho Break]的世界观的概念艺术 三上真司监督提出的,是对着重表现讲述内心恐怖的哪个世界观的创作和统一做了很大贡献的概念艺术. 这里以他经手的艺术为例,来看看稀少的恐 ...
- 【MySql】赶集网mysql开发36条军规
[MySql]赶集网mysql开发36条军规 2012-05-14 14:02:33 分类: Linux 写在前面的话: 总是在灾难发生后,才想起容灾的重要性: 总是在吃过亏后,才记得曾经有人提醒 ...
- Technical analysis of client identification mechanisms
http://www.chromium.org/Home/chromium-security/client-identification-mechanisms Chromium > Chro ...
- EFDC主控文件efdc.inp文件的xml格式化处理——转载http://blog.sina.com.cn/s/articlelist_1584892573_0_1.html
在对EFDC模型进行系统集成时,虽然可以通过一行一行读来进行解析处理,但那将是一个繁琐的工作.我们经过xml格式化处理后,再经xsd转成Dataset类对象,那么整个主控文件就是一个数据库表集合,对其 ...
- 面向对象与面向过程 $this的注意事项和魔术方法set和get
一.面向对象与面向过程的区别: 二者都是一种思想,面向对象是相对于面向过程而言的.面向过程,强调的是功能行为.面向对象,将功能封装进对象,强调具备了功能的对象.面向对象更加强调运用人类在日常的思维逻辑 ...
- Qt Focus事件,FocusInEvent()与FocusOutEvent()
描述:一开始我要实现的目的就是,在一个窗体上有多个可编辑控件(比如QLineEdit.QTextEdit等),当哪个控件获得焦点,哪个控件的背景就高亮用来起提示作用,查了下文档应该用focusInEv ...
- the differences between function and procedure
一.自定义函数: 1. 可以返回表变量 2. 限制颇多,包括 不能使用output参数: 不能用临时表: 函数内部的操作不能影响到外部环境: 不能通过select返回结果集: 不能update,del ...
- httpclient提交json参数
private void httpReqUrl(List<HongGuTan> list, String url) throws ClientProtocolException, IOEx ...