如何在不使用 segue 的情况下在视图控制器之间传递数据 [英] How to pass data between the view controllers without using segue

查看:35
本文介绍了如何在不使用 segue 的情况下在视图控制器之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用故事板开发 iPad 应用程序.在我的应用程序中,我使用 segue 模态演示从第一个视图控制器连接了一个模态视图控制器.通过单击模态视图中出现的一个按钮关闭模态视图.如何将一个字典从模态视图控制器传递到第一个视图控制器而不使用继续.

I am developing an iPad application using storyboard. In my application I have connected one modal view controller from first view controller using segue modal presentation.The modal view is dismiss by click one button appear in the modal view.How can i pass one dictionary from modal view controller to first view controller without using segue.

推荐答案

您可以使用 Notification 来传递值.

// Send Data    
   NSDictionary *aDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
                              anObject, @"objectName", 
                              anotherObject, @"objectId",
                              nil] autorelease];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"AnythingAtAll" object:nil userInfo:aDictionary];

您可以从观察到的入站通知中检索字典.在发布通知之前添加观察者.

//this might be in your init method or a viewDidLoad method of your FirstViewController

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(anyAction:) name:@"AnythingAtAll" object:nil];
enter code here

//获取数据

-(void)anyAction:(NSNotification *)anote
{
NSDictionary *dict = [anote userInfo];
AnyClass *objectIWantToTransfer = [dict objectForKey:@"objectName"];
}

请注意,您应该在 dealloc 方法中以观察者的身份移除您的对象.

[[NSNotificationCenter defaultCenter] removeObserver:self]

这篇关于如何在不使用 segue 的情况下在视图控制器之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆