在 iPhone 编程中传递的 ViewController 参数......如何? [英] Inter-ViewController parameters passing in iPhone programming... how to?

查看:17
本文介绍了在 iPhone 编程中传递的 ViewController 参数......如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到:iPhone:如何在 Tabbar App 中的多个 Viewcontroller 之间传递数据 并想知道

[[UIApplication sharedApplicaton] delegate]

extern struct* global

?

从概念上讲,我不明白 [[UIApplication sharedAppplicaton] 委托] 不是一个全球性的东西.事实上,现在使用脏的全局结构 * 可以减少内疚感.

Conceptually, I don't see how [[UIApplication sharedApplicaton] delegate] not being a global thing. In fact, that lessen the sense of guilty when using the dirty global struct * now.

我很快就要开始一个新项目.所以,我利用这个休息时间提出一个问题:是否有任何最佳实践代码示例来说明如何在两个 ViewController 之间(但不是全局)共享数据?

I am starting a new project very soon. So, I use this break to ask the question: is there any best-practice code example to illustrate how to share data between two ViewControllers (but not globally)?

让我举个例子:

  • 这是一个游戏
  • 有一个 NSString *name 来存储玩家的名字
  • 有一个 NSInteger 分数来存储玩家的当前分数
  • GameMainViewController 将更新并显示分数
  • 在 GameSettingViewController 中,有一个用于编辑名称的文本字段和一个用于重置分数的按钮
  • GameMainViewController 负责设置默认名称(如果为零),退出时保存名称和分数,启动时加载两者(如果存在)

所以

  • 我应该把姓名"和分数"放在哪里?
  • ViewController 如何访问和更改值

谢谢!

推荐答案

您可以在 NSUserDefaults.

检索项目:

NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];

设置项目:

[[NSUserDefaults standardUserDefaults]setObject:@"Horace" forKey:@"name"];

此外,如果这是您希望在应用程序启动时保留的数据,您可能需要将其存档到 plist 中.

Also, if this is data that you want to preserve across launches of the app, you may want to archive it into a plist.

这篇关于在 iPhone 编程中传递的 ViewController 参数......如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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