如何在不使用 segue 的情况下在视图控制器之间传递数据

编程入门 行业动态 更新时间:2024-10-24 19:21:28
本文介绍了如何在不使用 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 的情况下在视图控制器之间传递数据

本文发布于:2023-11-27 19:43:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   控制器   情况下   数据   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!