IOS 通知传值

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值