• 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;
        
    }


  • 相关阅读:
    SQL使用基本准则
    SQL使用技巧-或许你不知道的10条SQL技巧
    RabbitMQ安装遇到的坑
    编译安装python
    批量化杀死进程
    python中[-1]、[:-1]、[::-1]、[n::-1]使用方法
    c++中 . 和 -> 的区别是什么?
    C语言中的malloc、new、memset函数解析
    C语言中static关键字用法
    struct和typedef struct的区别
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/6952448.html
Copyright © 2020-2023  润新知