RootViewController.m

#import "ModalViewController.h"

@interface RootViewController ()

@end

@implementation RootViewController {

    ModalViewController *modalCtrl;

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad]; self.view.backgroundColor = [UIColor redColor]; UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 20)];
textLabel.tag = 100;
textLabel.backgroundColor = [UIColor orangeColor];
[self.view addSubview:textLabel]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 150, 100, 30);
[button setTitle:@"打开" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button]; modalCtrl = [[ModalViewController alloc] init]; //监听modal控制器的text属性
[modalCtrl addObserver:self forKeyPath:@"text"
options:NSKeyValueObservingOptionNew
context:NULL]; } //KVO触发方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"text"]) { NSString *text = [change objectForKey:@"new"];
UILabel *label = (UILabel *)[self.view viewWithTag:100];
label.text = text; } } - (void)buttonAction
{
[self presentViewController:modalCtrl animated:YES completion:NULL]; }

ModalViewController.m

@interface ModalViewController ()
{
NSString *_text;
}
@end @implementation ModalViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
} - (void)viewDidLoad
{
[super viewDidLoad]; self.view.backgroundColor = [UIColor greenColor]; UITextField *textFiled = [[UITextField alloc] initWithFrame:CGRectMake(50, 60, 160, 30)];
textFiled.tag = 100;
textFiled.delegate = self;
textFiled.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textFiled];
//显示键盘
[textFiled becomeFirstResponder]; UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(50, 150, 100, 30);
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button]; } //按钮点击事件
- (void)buttonAction
{
[self dismissViewControllerAnimated:YES completion:NULL]; // UITextField *field = (UITextField *)[self.view viewWithTag:100];
// NSString *text = field.text; // self.text = text; } #pragma UITextField delegate //点击return调用的协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField { //收起键盘
[textField resignFirstResponder]; return YES; } - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSLog(@"string:%@",string);
// string 是正在输入的最后一个字符 NSLog(@"%@",textField.text);
// textField.text 是输入的内容的前段字符(最后一个字符不包含) // UITextField *field = (UITextField *)[self.view viewWithTag:100];
// NSString *text = field.text;
NSString *str = [NSString stringWithFormat:@"%@%@",textField.text,string];
NSLog(@"str:%@",str);
self.text = str; return YES; }

KVO---视图间数据的传递:标签显示输入的内容【多个视图中】的更多相关文章

  1. Python多进程-进程间数据的传递

    两个进程间的数据是独立的,要进行数据传递的话可通过几个方法 Queue 通过队列来进行进程间数据的传递 # -*- coding:utf-8 -*- __author__ = "MuT6 S ...

  2. ASP.NET MVC5中View-Controller间数据的传递

    使用ASP.NET MVC做开发时,经常需要在页面(View)和控制器(Controller)之间传递数据,那么都有哪些数据传递的方式呢? 本文对于View向Controller中传值共列举了以下几种 ...

  3. Acitivity间数据的传递

    使用startActivityForResult方法进行数据传递.    MainActivity.java: public class MainActivity extends Activity { ...

  4. pyqt5:标签显示文本框内容

    文本框(lineEdit)输入文本,标签(label)就会显示文本框的内容. 原理如下: 输入文本时,lineEdit控件发射信号textChanged(),label收到后触发setText()槽. ...

  5. qtableview 鼠标划过单元格弹出标签显示单元格内容

    QStandardItem *item = new QStandardItem(show_content); infoTableModel->setItem(1, 1, item); item- ...

  6. Android零基础入门第83节:Activity间数据传递方法汇总

    在Activity间传递的数据一般比较简单,但是有时候实际开发中也会传一些比较复杂的数据,本节一起来学习更多Activity间数据的传递. 一.常用数据类型 在前面几节我们只学习了一些常用类型的数据传 ...

  7. C#不同窗体间数据传递

    在做项目中经常会使用不同窗体之间的值,所以就有了传值的概念.最常见的是父子窗体之间的数据传递,比如登录ID,各个窗体都需要知道. 1.       如果很多窗体都需要用到某一窗体的东西,比如登录窗体记 ...

  8. Struts2(六.用标签显示用户列表及Value Stack和Stack Context)

    一.用Struts2标签显示用户列表 原理: 在struts中可以通过在action中将所有用户的信息存入到某个范围中,然后转向userlist.jsp,进行访问 原则: 在jsp网页上,尽量不要出现 ...

  9. django基础之day09,Forms组件在程序中做了哪些事? 校验数据、渲染标签、展示信息

    ******************************* Forms组件 *************************************************** Forms组件在 ...

随机推荐

  1. mysql 年龄计算(根据生日字段)

    mysql 年龄计算(根据生日字段) year( from_days( datediff( now( ), birthdate))) //获取年龄 now() 当前时间,精确到秒 datediff(b ...

  2. 前端HTML中float学习笔记

    float元素原本的作用是用来使文字包裹图片,现在人们更多的是用来进行布局(ps:有没有点滥用的意思) 也就是说本来你排好的界面设计,但是因为浮动会导致元素脱离文档流,使得其他非浮动的块级元素会无视这 ...

  3. 实现Brush对象的五种图形

    本实例将使用Graphics类绘制五种图形来分别演示SolidBrush.HatchBrush.TextureBrush.LinearGradientBrush.PathGradientBrush这五 ...

  4. Android_方向传感器

    Android方向传感器小案例,主要代码如下: package com.hb.direction; import android.app.Activity; import android.conten ...

  5. WCF分佈式事務支持

    WCF分佈式事務對Binding有要求,不支持BasicHttpBinding,BasicHttpContextBinding,NetPeerTcpBinding 要支持分佈式事務,需要進行以下配置: ...

  6. 3、scala函数入门

    1.定义函数 2.在代码块中定义函数体 3.递归函数与返回类型 4.默认参数 5.带名参数 6.变长参数 7.使用序列调用变长参数  8.过程 9.lazy值              10.异常 1 ...

  7. Windows 10 新功能

    一.与 Cortana 集成的便笺 借助便笺,你可捕捉并保存绝妙创意或记录重要细节.便笺现已与 Cortana 集成,让你能够设置整个设备中的提醒. (一)   先来了解一下微软小娜Cortana. ...

  8. 图像的全局特征--HOG特征、DPM特征

    HOG特征:方向梯度直方图(Histogram of Oriented Gradient,)特征是一种全局图像特征描述子. 它通过计算和统计图像局部区域的梯度方向直方图来构成特征.Hog特征结合SVM ...

  9. 安卓JNI使用C++类

    安卓JNI使用C++类,同时可使用C++的类成员变量,这就必须保证程序持续保存Native状态,即长期维护C++对象的地址.完成初始化之后,需要使用对象成员的java层函数需要传入对象的地址. 一.N ...

  10. 【sqli-labs】 less13 POST - Double Injection - Single quotes- String -twist (POST型单引号变形双注入)

    报错 闭合掉括号 这关登录成功之后不显示登录的用户名密码了