在iPhone编程中传递Inter-ViewController参数......怎么样? [英] Inter-ViewController parameters passing in iPhone programming... how to?

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

问题描述

我读到: iPhone:如何在Tabbar应用程序中的几个ViewController之间传递数据,并想知道

[[UIApplication sharedApplicaton] delegate]

extern struct* global

从概念上讲,我不知道[[UIApplication sharedApplicaton]委托]如何不是全局性的。事实上,现在使用脏全局结构*会减轻内疚感。

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 *名称来存储玩家的名字

  • 有一个NSInteger分数来存储玩家的当前分数

  • GameMainViewController将在GameSettingViewController中更新并显示分数

  • ,有一个用于编辑名称的文本字段和一个用于重置分数的按钮

  • GameMainViewController负责设置默认名称(如果为nil),退出时保存名称和分数,开始时加载两者(如果存在)

  • this is a game
  • there is a NSString *name to store the player's name
  • there is a NSInteger score to store the player's current score
  • the GameMainViewController will update and display the score
  • in the GameSettingViewController, there is a text field to edit name and a button to reset score
  • the GameMainViewController is responsible for set a default name (if nil), save both name and score when exit, load both (if exists) when start

所以


  • 我应该在哪里放名字和得分?

  • ViewControllers如何访问和更改值

谢谢!

推荐答案

您可以在 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编程中传递Inter-ViewController参数......怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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