文章目录
  1. 1. 利用通知
  • 代理是一种简单而功能强大的设计模式,这种模式用于一个对象“代表”另外一个对象和程序中其他的对象进行交互。 主对象(这里指的是delegating object)中维护一个代理(delegate)的引用并且在合适的时候向这个代理发送消息。这个消息通知“代理”主对象即将处理或是已经处理完了某一个事件。这个代理可以通过更新自己或是其它对象的UI界面或是其它状态来响应主对象所发送过来的这个事件的消息。或是在某些情况下能返回一个值来影响其它即将发生的事件该如何来处理。代理的主要价值是它可以让你容易的定制各种对象的行为。注意这里的代理是个名词,它本身是一个对象,这个对象是专门代表被代理对象来和程序中其他对象打交道的。
1
2
3
4
5
6
7
8
9
10
11
12
13
// RootViewController.h
# import "ModalViewContorller.h"
@optional
//optional 强制必须实现的方法
-(void)changeLabelText:(NSString *)text;
@end
@interface ModalViewController:UIViewController
{
@private
UITextField *textFiled;
}
@property(nonatomic,assign)id <ModalViewControllerDelegate> delegate;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// RootViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag = 101;
button.frame = CGRectMake(320/2-140/2,200,140,40);
[button setTile:@"进入" forState:UIControlStateNormal];
[button addTarget:self action:@selector(presentModalVC) forControlEvent:UIControlEventTouchUpInside];
[self.view addSubiew:button];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(90,100,140,40)];
label.text = @"hello world";
label.tag = 102;
label.textAligment = NSTextAligmentCenter;
[self.view addSubView:label];
}
-(void)presentModalVC
{
ModalViewController *modalVc = [[ModalViewController alloc] init];
modalVc.delegate = self;
// 动画效果
modalVc.modalTranstionSyle = UIModalTransitionStylePatialCurl;
if([[UIDevice currentDevice].systemVersion floatValue] < 6.0){
[self.presentModalViewController:modalVc animated:YES];
}else{
[self.presentModalViewController:modalVc animated:YES completion:^{
NSLog(@"call back");
}];
[modalVC relese];
}
}
-(void)changeLabelText:(NSString *)text
{
UILabel *label = (UILable *)[self.view viewWithTag:102];
label.text = text;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
// ModalViewController.h
@protocol ModalViewControllerDelegate <NSObject>
@optional
//optional 强制必须实现的方法
-(void)changeLabelText:(NSString *)text;
@end
@interface ModalViewController:UIViewController
{
@private
UITextField *textFiled;
}
@property(nonatomic,assign)id <ModalViewControllerDelegate> delegate;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//ModalViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];

*textField = [[UIText alloc] initWithFrame:CGRectMake(70,100,100,30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(320/2-140/2,100,140,40);
[button setTitle:@"dismiss" forState:UIControlEvent:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)dismiss
{
if([self.delegate respondsToSelector:@selector(changeLabelText:)]){
[self.delegate changeLabelText:textField.text];
}
// 将模态视图关闭
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"dismiss")];
}];
}

利用通知

1
2
3
// 在通知中心注册一条名为ChangeLabelTextNotification的通知
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeLabelText:) name:@"ChangeLabelTextNotification" object:nil];
// deallco时需要删除
1
2
3
4
5
6
-(void)changeLabelText:(NSNotification *)notification
{
id text = notification.object;
UILabel *label = (UILabel *)[self.view viewWithTag:102];
label.text = text;
}
1
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeLabelTextNotification" object:_textField.text]
文章目录
  1. 1. 利用通知