【发布时间】:2015-08-14 14:25:16
【问题描述】:
我正在尝试制作一个用户回答问题的小游戏。整体流程是这样的:
- 通过单击按钮选择难度级别(push segue)
- 玩家 1 输入名称,按“下一步”(push segue)
- 回答问题
- 回答问题
- 完成。
- 玩家 2 输入名称,按“下一步”(push segue)
- 回答问题
- 回答问题
- 完成。
- 看看大家的回答
- 被送回#1
我的问题是我真的不需要将用户信息(姓名、答案等)存储在数据库中,因为这些数据在回合结束后就没用了。但是,我需要它足够持久,以便我可以在回合结束之前跨视图控制器访问该数据。
对于难度级别,我使用 AppDelegate 上的自定义属性来保持设置:
AppDelegate.h
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSString *difficultyLevel;
@end
DifficultyViewController.m
- (IBAction)setDifficultyLevel:(id)sender {
UIButton *button = (UIButton *)sender;
NSString *difficulty = [[button titleLabel] text];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.difficultyLevel = difficulty;
}
PlayerProfileViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"Edmund: %@", appDelegate.difficultyLevel);
}
但是,这感觉不是很有可扩展性,因为我将拥有许多其他属性,而且我不觉得 AppDelegate 的用途是什么。
有没有一种通用的方法来为这类事情持久化数据?
【问题讨论】:
-
为什么不创建一个单例类“SharedData”并将所有数据存储在那里?
标签: ios objective-c