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

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;

}
时间: 2024-08-08 15:33:57

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

代理---视图间数据的传递:标签显示输入的内容【多个视图中】

RootViewController.h #import "ModalViewController.h" @interface RootViewController : UIViewController<ModalViewDelegate> RootViewController.m @interface RootViewController () @end @implementation RootViewController { ModalViewController *m

通知---视图间数据的传递:标签显示输入的内容【多个视图中】

RootViewController.m - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { //注冊通知 [[NSNotificationCenter defaultCenter] addObserver:self selec

单例---视图间数据的传递:标签显示输入的内容【多个视图中】

RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor greenColor]; //创建显示文字的label UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 40)]; label.tag = 102; label.backgroundColor = [

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

使用ASP.NET MVC做开发时,经常需要在页面(View)和控制器(Controller)之间传递数据,那么都有哪些数据传递的方式呢? 本文对于View向Controller中传值共列举了以下几种方式: QueryString RouteData Model Binding Form 使用和Action参数同名的变量进行传递 Cookie 对于Controller向View中传值则列举了以下几种方式: 单个值的传递 Json 匿名类型 ExpandoObject ViewBag.ViewDa

浅析:Acitivity间数据的传递

使用startActivityForResult方法进行数据传递. MainActivity.java: 1 public class MainActivity extends Activity { 2     Button  btn; 3     @Override 4     protected void onCreate(Bundle savedInstanceState) { 5         super.onCreate(savedInstanceState); 6        

Acitivity间数据的传递

使用startActivityForResult方法进行数据传递.    MainActivity.java: 1 public class MainActivity extends Activity { 2 Button btn; 3 @Override 4 protected void onCreate(Bundle savedInstanceState) { 5 super.onCreate(savedInstanceState); 6 setContentView(R.layout.ac

select 标签 未选择任何内容 在 vue.js 中使用遇到的坑

select 标签使用时,经常会遇到不进行任何的选择,即 value=‘’ 的情况 这时如果配合  vue.js 的 v-model 使用,会使得 v-model 标签失效,也就是说,v-model 标签配合 select 不能存在  <option value="" ></option>的情况 但是如果,我们必须有什么也不选择的选项的话,我们可以 <option value="%" >点击选择...</option>

excel限制数据的输入选项如何限制输入的内容

在某些字段中可能只要求输入特定的几个数据,此时如何限制输入的内容呢?(常见问题)excel表格里对数据输入的限制是如何实现的excel2013下拉栏怎样能禁止输入,只能选择下拉框里的选项excel如何只能从下拉式菜单中选择输入项而不能手动输入[wangqiang博客推荐视频教程点击下方链接进入] 1045432700博客推荐视频教程EXCEL从入门到精通`` 原文地址:http://blog.51cto.com/13172026/2287788

WPF,Silverlight与XAML读书笔记第十五 - 页间导航 页间数据传递

?说明:本系列基本上是<WPF揭秘>的读书笔记.在结构安排与文章内容上参照<WPF揭秘>的编排,对内容进行了总结并加入一些个人理解. 导航 有关导航的话题在介绍NavigationWindow与Page等元素时有提及.这篇文章将详细分析导航相关话题.同其它话题,针对WPF,Silverlight与WP 7,导航特性大致相似又有着些许不同.在介绍此特性时相同的特性将合在一起,每个框架独有的特性也将独立介绍并有明显标识. 导航的功能及目的就是从一个页面转向另一个页面,可能是前进亦或是后