#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. IntelliJ IDEA(社区版)学习记录

    一.下载 地址:官网下载地址 二.安装 运行安装程序,一路下一步.注意选择安装路径. 三.基本概念 project:相当于donet下的解决方案 module:相当于donet下的项目工程 四.IDE ...

  2. Windows与Linux/Mac系统时间不一致的解决方法

    Windows与Linux/Mac系统时间不一致的解决方法 分类: linux2012-02-12 14:25 1691人阅读 评论(1) 收藏 举报 windowsubuntusystemlinux ...

  3. HTML: a的僞類

    a:link a:visited a:hover a:active css中能夠分別針對a鏈接的4中狀態進行css樣式設置,這四種狀態就是a的僞類,分別是: 普通,訪問後,鼠標放在a鏈接上,鼠標按下未 ...

  4. 性能监控工具nmon

    工具集: Nmon:性能数据收集分析工具 Nmon analyser:性能数据分析工具,excel文件 nmon概述:     nmon是收集AIX或Linux主机的性能数据并分析的工具,使用简单易用 ...

  5. Nginx 日志文件切割

    Nginx 是一个非常轻量的 Web 服务器,体积小.性能高.速度快等诸多优点.但不足的是也存在缺点,比如其产生的访问日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将 导致日志文件容量 ...

  6. Virtual Memory DEMAND PAGING - The avoidance of thrashing was a major research area in the 1970s and led to a vari- ety of complex but effective algorithms.

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION With the use of pagin ...

  7. Linux下安装Xdebug

    为了调试PHP程序,安装一下xdebug. 官方网址: http://www.xdebug.org 首先下载xdebug2.1.0,在官方首页下载源代码,下载回来的文件名是:xdebug-2.1.0. ...

  8. 策略模式代替大量的if else

    原代码 public class Example { public Double calRecharge(Double charge, RechargeTypeEnum type) { if (typ ...

  9. Summary of java stream classes

    Java’s stream classes are good for streaming sequences of bytes, but they’re not good for streaming ...

  10. java Reentrant Lock

    //Listing 7-1. Achieving Synchronization in Terms of Reentrant Locks import java.util.concurrent.Exe ...