KVO---视图间数据的传递:标签显示输入的内容【多个视图中】
2014-10-29 13:27
399 查看
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---视图间数据的传递:标签显示输入的内容【多个视图中】
- 通知---视图间数据的传递:标签显示输入的内容【多个视图中】
- 通知---视图间数据的传递:标签显示输入的内容【多个视图中】
- 单例---视图间数据的传递:标签显示输入的内容【多个视图中】
- 单例---视图间数据的传递:标签显示输入的内容【多个视图中】
- 代理---视图间数据的传递:标签显示输入的内容【多个视图中】
- 代理---视图间数据的传递:标签显示输入的内容【多个视图中】
- 标签显示输入的内容【同一个视图中】
- 页面显示内容按照输入时换行显示标签
- 模拟邮箱输入邮箱地址、收藏标签。input框输入内容后回车,内容显示成小方块并带删除按钮。
- 一步步学习SPD2010--第五章节--处理数据视图(5)--显示和隐藏内容
- AngularJS,实现输入框失焦添加内容到最前,当数据重复删除原来并显示在数组最前
- EF5+MVC4系列(7) 后台SelectListItem传值给前台显示Select下拉框;后台Action接收浏览器传值的4种方式; 后台Action向前台View视图传递数据的四种方式(ViewDate,TempDate,ViewBag,Model (实际是ViewDate.Model传值))
- iframe标签引用显示为空,ie下 【此内容不能显示在一个框架中, 为了帮助保护在此网站中输入的信息的安全……】
- 在Action中查询出SQL SERVER表的数据,返回一个list。怎么在JSP页面中使用Struts标签,显示出表中的内容?(转)
- MFC编辑框输入内容与显示内容转为16进制数据——更正版
- HTML,angularJS表格内容排序,点击删除,删除当前,输入内容显示包含内容的数据
- Java后台去除前台传递数据中的页面标签(HTML,Javascript,Style),获取文本内容!
- 一步步学习SPD2010--第五章节--处理数据视图(5)--显示和隐藏内容
- 窗体上有两个文本框:一个文本框中最多输入字符6个;一个文本框中输入任何内容都显示*号。再添加一个按钮、2个单选按钮。实现单击按钮后,根据单选按钮,将对应文本框中内容显示在标签