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. python 端口扫描程序

    #! /usr/bin/env python3 #-*- coding:utf-8 -*- import socket import threading OPEN_COUNT = 0 lock = t ...

  2. POJ 1010 题目翻译+题解

    题目实在是太难懂了,我翻译了一下... STAMPS Description Have you done any Philately lately? 你最近有没有集邮? You have been h ...

  3. python--1、入门

    python的创始人为吉多·范罗苏姆(Guido van Rossum). python在2017年统计的所有语言排名中处于第四名,稳步上升状态. python应用领域: WEB开发(Django框架 ...

  4. Centos 自动删除日志文件的Shell代码

    #!/bin/bash # #判断文件夹内文件的大小,如果大于一定的数值,那么删除 # echo '判断文件夹内文件的大小,如果大于一定的数值,并且文件名称包含数字(年月日)的删除,那么删除' pat ...

  5. 后端springmvc,前端html5的FormData实现文件断点上传

    前言 最近项目中有使用到文件断点上传,得空便总结总结,顺便记录一下,毕竟“好记性不如烂笔头”. 后端代码: package com.test.controller; import java.io.Bu ...

  6. Java数据的基本类型

    整数类型: byte:8位(1个字节) eg:byte x=2,y: 错误实例:byte b:b=b+3:   其中b=b+3是错误的,应该是b=(byte)(b+3)强制转换: short:16位( ...

  7. halcon 模板匹配 -- create_shape_model

    create_shape_model(Template : : //reduce_domain后的模板图像 NumLevels,//金字塔的层数,可设为“auto”或0—10的整数 AngleStar ...

  8. VHDL之concurrent之when

    WHEN (simple and selected) It is one of the fundamental concurrent statements (along with operators ...

  9. php知识点(基本上文档都有,只为方便记忆)

    类型转换 (unset)转换为NULL (binary) 转换和 b 前缀转换支持为 PHP 5.2.1 新增   转换二进制 隐藏php后缀名 AddType application/x-httpd ...

  10. [Advanced Algorithm] - Validate US Telephone Numbers

    题目 如果传入字符串是一个有效的美国电话号码,则返回 true. 用户可以在表单中填入一个任意有效美国电话号码. 下面是一些有效号码的例子(还有下面测试时用到的一些变体写法): 555-555-555 ...