第一个viewController:
- - (IBAction)pressed:(id)sender {
- // [self performSegueWithIdentifier:@"second" sender:self];
- NSLog(@"send message:%@",firstField.text);
- //页面跳转传值方法二:利用notification
- NSDictionary *dicts = [NSDictionary dictionaryWithObjectsAndKeys:@"one1",@"one",@"two2",@"two",@"three3",@"three", nil];
- //注册(第一步)
- NSNotification *notification =[NSNotification notificationWithName:@"mynotification" object:firstField.text];
- //发送(第二步)
- [[NSNotificationCenter defaultCenter] postNotification:notification];
- //注册+发送也可以一行完成(等效于以上两行)
- [[NSNotificationCenter defaultCenter] postNotificationName:@"mynotification2" object:dicts];//发送一个字典过去
- }
第二个viewController:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- //接受端:接受(第一步)
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler:) name:@"mynotification" object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationHandler2:) name:@"mynotification2" object:nil];
- }
- //自定义接收信息和处理的方法(第二步)
- -(void) notificationHandler:(NSNotification *) notification{
- secondField.text = [notification object];//收到消息后在UItextField中显示出来
- }