#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在两个页面间的简单传值的更多相关文章

  1. iOS 页面间几种传值方式(属性,代理,block,单例,通知)

    第二个视图控制器如何获取第一个视图控制器的部分信息 例如 :第二个界面中的lable显示第一个界面textField中的文本 这就需要用到属性传值.block传值 那么第一个视图控制器如何获的第二个视 ...

  2. 转:在两个页面间翻转设置Animation动作的一些总结

    今天碰到两个页面之间翻转的动作设计问题,发现了一些问题,故做个总结,很多都写在注释部分: 1.首先,我们来手动创建两个view以及相应的viewController.是手动,不是用IB (1)刚开始只 ...

  3. 使用block实现两个页面之间的传统价值观

    第二个view声明一个block属性: @property (nonatomic, copy) void(^doTransferMsg)(NSString *_msg); 然后传值方法里检查block ...

  4. 【vue】两个页面间传参 - props

    目录 Step1 设置可以 props 传递数据 Step2 跳转前页面中传递数据 Step3 跳转后的页面接收数据 从 A 页面跳转到 B 页面, 参数/数据通过 props 传递到 B 页面,这种 ...

  5. NavigationViewController页面间通信及传值

    使用进行页面跳转时,应该使用方法来跳转至下一页面,这样的话,下一页面同样在容器中. 1AloneSetPrizeViewController *setPrize = [[AloneSetPrizeVi ...

  6. 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错

    原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...

  7. iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)

    iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...

  8. iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)   iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...

  9. iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...

随机推荐

  1. [办公自动化]windows7 仿宋GB2312字体打印不对

    今天同事发现自己的文档中,仿宋GB2312这种字体打印的看着总觉得不对. 他使用的操作系统是windows7,office 是2007. 解决方案: 将windows xp中的c:\windows\f ...

  2. [ZZ] GTX 280 GPU architecture

    http://anandtech.com/show/2549 Now that NVIDIA’s has announced its newest GPU architecture (the GeFo ...

  3. Unity 中 使用c#线程

    使用条件   天下没有免费的午餐,在我使用unity的那一刻,我就感觉到不自在,因为开源所以不知道底层实现,如果只是简单的做点简单游戏,那就无所谓的了,但真正用到实际地方的时候,就会发现一个挨着一个坑 ...

  4. wordpress 常用函数 checked(),selected(),disabled()

    checked().selected().disabled(),这三个函数是主题设计和插件设计中添加后台设置比较常用到的函数. 例如自定义一个widget,这个widget有一个字段为文章排列方式.文 ...

  5. Cacti安装详细步骤

    原文链接: https://www.centos.bz/2012/01/cacti-install-tutorials/ Cacti-监控MySQL: http://www.cszhi.com/201 ...

  6. SET ? DECLARE

    http://dev.mysql.com/doc/refman/5.7/en/declare-local-variable.html http://dev.mysql.com/doc/refman/5 ...

  7. 【转】CodeIgniter定义自己的Helper和Helper的方法

    最近做程序,采用了PHP+CodeIgniter框架,总体来说这框架很轻而且上手也很快的. 首先,说下如何定义自己的Helper,个人理解helper其实就是定义函数方法. 如果要建个全新的自己的He ...

  8. hdc cdc

    CWindowDC dc(this); HDC hdc=dc.GetSafeHdc(); using namespace Gdiplus; Graphics graphics(hdc); graphi ...

  9. Region的预分区

    1.预分区的方式 共有四种方式 2.帮助信息 help 'create' 3.第一种方式 4.在页面上查看效果(端口号:60010) 5.第二种方式 )创建文件,并在文件中书写分区的值 )创建表 6. ...

  10. Intersection of Two Linked Lists | LeetCode

    利用两个栈,然后分别存储每一个链表. 继而,相继pop相同的节点. 有些细节需要注意,请看最后的返回值是如何处理的. /** * Definition for singly-linked list. ...