RootViewController.h
1 #import <UIKit/UIKit.h>
2 #import "DetailViewController.h"
3 @interface RootViewController : UIViewController<ChangeDelegate>
4 {
5 UITextField *tf;
6 }
7 @end
RootViewController
.m
1 #import "IndexViewController.h"
2 #import "DetailViewController.h"
3 #import "AppStatus.h"
4
5 @implementation IndexViewController
6
7 -(void)dealloc
8 {
9 [[NSNotificationCenter defaultCenter] removeObserver:self
10 name:@"CHANGE_TITLE" object:nil];
11 [super dealloc];
12 }
13
14 -(id)init
15 {
16 if (self = [super init])
17 {
18 [[NSNotificationCenter defaultCenter] addObserver:self
19 selector:@selector(change:)
20 name:@"CHANGE_TITLE"
21 object:nil];
22 }
23 return self;
24 }
25
26 -(void)change:(NSNotification *)aNoti
27 {
28 // 通知传值
29 NSDictionary *dic = [aNoti userInfo];
30 NSString *str = [dic valueForKey:@"Info"];
31
32 UITextField *tf = (UITextField *)[self.view viewWithTag:1000];
33 tf.text = str;
34 }
35
36 -(void)viewWillAppear:(BOOL)animated
37 {
38 [super viewWillAppear:animated];
39 /*
40 // 单例传值
41 UITextField *tf = (UITextField *)[self.view viewWithTag:1000];
42 tf.text = [AppStatus shareInstance].contextStr;
43 */
44 }
45
46 @end
DetailViewController
.h
1 #import <UIKit/UIKit.h>
2 @protocol ChangeDelegate;//通知编译器有此代理
3
4 @interface DetailViewController : UIViewController
5 {
6 UITextField *textField;
7 }
8 @end
DetailViewController
.m
1 #import "DetailViewController.h"
2 #import "AppStatus.h"
3
4 @implementation DetailViewController
5 @synthesize naviTitle = _naviTitle;
6
7 -(void)loadView
8 {
9 UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:self action:@selector(doneAction:)];
10 self.navigationItem.rightBarButtonItem = doneItem;
11 [doneItem release];
12 }
13
14 // pop回前一个页面
15 -(void)doneAction:(id)sender
16 {
17 NSDictionary *dic = [NSDictionary dictionaryWithObject:textField.text forKey:@"Info"];
18
19 [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_TITLE" object:nil userInfo:dic];
20
21 [self.navigationController popViewControllerAnimated:YES];
22
23 }