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 ...
随机推荐
- CSS系列:less备忘
less备忘 //这是一个运行在koala中的less文件,//注释不会被编译到css文件中,/**/注释会 ****************by 李可 2016/04/19 /*所有,所有伪类*/ ...
- GC-垃圾回收
代:0代,1代,2代: 所谓第几代,指经历过GC回收的次数. 回收算法: 1.确认需要检查的代. 在分配新对象时, 如果第0代已满,则进行检查:如果第1代已满,则进行检查:第2代同理: 如第0代没有足 ...
- PHP 错误与异常 笔记与总结(3)PHP 配置文件(php.ini)中与错误相关的选项 与 设置错误级别
[PHP 配置文件中与错误相关的选项 ] 选项 描述 error_reporting 设置错误报告的级别 display_errors 是否显示错误 log_errors 设置是否将错误信息记录到日志 ...
- PHP 单例模式代码片段
<?php error_reporting(E_ALL | E_STRICT); class single{ public $hash; static protected $ins = null ...
- 一个项目软件的大小基本都占用在外部引用的jar包上了。
1.一个项目几百兆,基本都是外部jar包,引用的. 2.自己本身业务代码并没有那么多的 3.看下meven的仓库大小就知道了,都几百兆
- 一些App的User-Agent
天猫 Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; MI 2C Build/KTU84P) AppleWebKit/537.36 (KHTML, like ...
- maven run as(debug as)没有运行的选项时
run as - run configration -maven build- goal目录下填上:tomcat:run即可
- Linguistic corpora 种子语料库-待分析对象-分析与更新语料库
Computational Linguistics http://matplotlib.org/ https://github.com/matplotlib/matplotlib/blob/maste ...
- P2296 寻找道路
#include <bits/stdc++.h> using namespace std; const int maxn = 10005; set<int> to[maxn]; ...
- url如何传递参数
$(document).ready(function() { var name=getQueryString('minename'); if (name != null && name ...