</pre><pre name="code" class="objc"><pre name="code" class="objc">
//
// ViewController.h
//
// Created by dllo on 15/8/25.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// UI20_KVO
//
// Created by dllo on 15/8/25.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "ViewController.h"
#import "Student.h"
#import "secondViewController.h"
@interface ViewController ()
@property(nonatomic,retain)Student *stu;
@property(nonatomic,retain)UITextField *myTextField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.stu=[[Student alloc] init];
/// kvc Key-Value-Coding 键值对
/// kvo Key-Value-Observer 键值观察者,可以监控对象里的属性的变化,只有值发生了变化就会触发方法..
//监听属性的值得变化,.一定要用设置器,否则监听失败..
//注册一个监听
[self.stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:@"这是监控的文本"];
self.stu.name=@"白子画";
NSLog(@"花千骨");
//创建一个button,然后能push到下一页..
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(100, 100, 100, 20);
[button setTitle:@"下一页" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
///传值得第四种方法..通知中心
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receive:) name:@"test" object:nil];
self.myTextField=[[UITextField alloc] initWithFrame:CGRectMake(100, 200, 150, 50)];
self.myTextField.layer.borderWidth=2;
self.myTextField.layer.cornerRadius=10;
self.myTextField.layer.masksToBounds=YES;
[self.view addSubview:self.myTextField];
[_myTextField release];
///第一种监听方式
// [self.myTextField addTarget:self action:@selector(action:) forControlEvents:UIControlEventEditingChanged];
///第二种,通知中心监听输入内容
//第一个参数:添加监听者的对象,一般都是当前文件的对象,self
//第二个参数:监听触发的方法
//第三个参数:监听的名,如果对TextField进行监听,TextField提供了专门的常量字符串,127行
//第四个参数:把要监听的对象放到最后一位,,用于传值,则是nil
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeValue:) name:UITextFieldTextDidChangeNotification object:self.myTextField];
}
-(void)changeValue:(NSNotification *)nitification{
NSLog(@"%@",self.myTextField.text);
//这个方法用来判断电话号码的正则表达式,正则表达式通过自己的语法规则可以用一串表达式判断身份证号等信息,如果需要,就百度....
NSString *str=@"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
//用谓词来判断当前输入的文本框内容和正则表达式格式是否吻合
NSPredicate *cate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",str];
//然后进行判断,返回一个bool类型的结果
BOOL result=[cate evaluateWithObject:self.myTextField.text];
if (result) {
NSLog(@"符合条件");
}else{
NSLog(@"输入错误");
}
}
-(void)click:(UIButton *)button{
// secondViewController *cecVc=[[secondViewController alloc] init];
// [self.navigationController pushViewController:cecVc animated:YES];
// [cecVc release];
UIAlertController *aletVC=[UIAlertController alertControllerWithTitle:@"提示" message:@"ok" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:aletVC animated:YES completion:nil];
//给他添加textfield
[aletVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder=@"账号";
//把监听写在textfield的添加方法里
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText:) name:UITextFieldTextDidChangeNotification object:textField];
}];
//添加两个按钮
UIAlertAction *sureAction=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//在里面写点击方法
UITextField *textField=aletVC.textFields[0];
//打印输入内容
NSLog(@"%@",textField.text);
}];
[aletVC addAction:sureAction];
//设置使按钮用不了
sureAction.enabled=NO;
}
- (void)changeText:(NSNotification *)notification{
UIAlertController *alertVC=(UIAlertController *)self.presentedViewController;
UITextField *textField=alertVC.textFields[0];
UIAlertAction *action=alertVC.actions[0];
action.enabled=textField.text.length > 5;
}
//把通知中心的值带回到之前设置的方法里
-(void)receive:(NSNotification *)notification{
NSLog(@"%@",notification.userInfo);
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
//只要监听的属性内容发生了变化,就会马上触发这个方法..
NSLog(@"%@",change);
}
- (void)dealloc{
如果添加了观察者,就要在方法里把对应的观察者移除,,如果不移除的话.会造成内存上的问题
//监听textfield,,移除监听
[[NSNotificationCenter defaultCenter] removeObserver:self];
//传值,移除监听中心
[self.stu removeObserver:self forKeyPath:@"name"];
[_stu release];
[super dealloc];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
//
// secondViewController.h
// UI20_KVO
//
// Created by dllo on 15/8/25.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface secondViewController : UIViewController
@end
//
// secondViewController.m
// UI20_KVO
//
// Created by dllo on 15/8/25.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "secondViewController.h"
@interface secondViewController ()
@end
@implementation secondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//创建一个button,然后能push到下一页..
UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
button.frame=CGRectMake(100, 100, 100, 20);
[button setTitle:@"上一页" forState:UIControlStateNormal];
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)click:(UIButton *)button{
[self.navigationController popViewControllerAnimated:YES];
NSDictionary *dic=@{@"1":@"2",@"3":@"4"};
//通知中心反方向传值
[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:dic];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
//
// Student.h
// UI20_KVO
//
// Created by dllo on 15/8/25.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property(nonatomic,copy)NSString *name;
@end
//
// Student.m
// UI20_KVO
//
// Created by dllo on 15/8/25.
// Copyright (c) 2015年 flg. All rights reserved.
//
#import "Student.h"
@implementation Student
@end
KVO
最新推荐文章于 2024-06-14 22:07:23 发布