1

我在这里有一个功能,在完成一轮后,如果您的分数高于默认分数条目或新放置的高分,那么它将与您的数据交换其数据并将其他所有内容向下推。从列表中删除最后一个条目。目前这只是一个交换,为了功能,我将对其进行硬编码,然后再对其进行重构。

我的主要问题是,当我设置一个文本输入视图来捕获玩家名称时,在没有玩家输入的情况下立即继续执行并导致游戏崩溃。我注释掉了设置文本的行,因为我有一个默认值,以防我尝试进行的任何尝试失败。如何让执行在输入时等待片刻?我必须设置一个委托方法吗?如果是这样,代表们仍然有点困惑。我可以设置它来工作,但我不明白它,所以我不能用它做任何其他特殊的自定义任务。我已经研究了一段时间,没有进一步...

-(void)saveData:(ScoreKeep *)stats{
NSMutableDictionary *swap = [[NSMutableDictionary alloc]init];//used for swaping entries
NSString *filePath = [self pathOfFile];
NSLog(@"Writing to %@", filePath);
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) {
    NSLog(@"Loading previous dictionary to save...");
    dataDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    if ([dataDictionary objectForKey:@"1"]) {
        NSMutableDictionary *highScore = [dataDictionary objectForKey:@"1"];
        if ([stats.score intValue] > [[highScore objectForKey:@"SCORE"] intValue]) {
            NSLog(@"You Win! score: %@ highscore: %@", stats.score,[NSNumber numberWithInt:[[highScore objectForKey:@"SCORE"] intValue]] );


            stats = [[ScoreKeep alloc] initWithNibName:@"Scorekeep" bundle:nil];
            NSLog(@"Setting up name entry");
            [self.view addSubview:stats.view]; //New view is added so that the player can input data(Assume it is complete);

            //stats.nameTag = setName.nameTag;//This line is executed before the new view is dismissed causing an error to occur
            [stats setupDictionary]; // It just goes down hill from here if the previous line is uncommented
            [dataDictionary setObject:stats.sComponents forKey:@"1"];
        }else {

            NSLog(@"You Lose: %@ highscore: %@", stats.score,[NSNumber numberWithInt:[[highScore objectForKey:@"SCORE"] intValue]] );
        }


        NSLog(@"Got first place entry");
    }else {

        NSLog(@"Initilizing Score");

    }


}else{
    NSLog(@"Creating new dictionary to save...");
    dataDictionary = [[NSMutableDictionary alloc]init];
}


[dataDictionary writeToFile:filePath atomically:YES];

}

帮助将不胜感激。如果需要更多信息,我很乐意提供。顺便说一句,ScoreKeep 是一个对象,它包含一个字典和一个创建字典的函数,以便它可以设置我需要的任何值并将它们打包到 sComponents(要输入到主保存文件中的字典)

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
@class omphalosUtility;
#pragma mark -
#pragma mark Saving data
#pragma mark -

static inline void poop(){
    NSLog(@"POOP");

}

我将尝试制作一个独立于应用程序工作的实用程序文件,以便我可以更新文件并执行其他通用操作,例如在需要时保存。这是朝着我想要采取的方向迈出的一步。

4

1 回答 1

0

如果我做对了,(代码真的很讨厌,伙计......)你的问题是你试图以错误的方式呈现视图控制器。

如果我错了,请纠正我,ScoreKeep 是 ViewController 吗?如果是这样,您必须正确命名它。这是一个开始。

其次,您不能仅通过将其“视图”属性添加到当前视图控制器的视图层次结构来呈现另一个视图控制器。这样视图将无法正确响应事件。

正确的方法是通过模态显示 ScoreKeep ViewController。

如果不使用委托,没有其他正确的方法可以做到这一点。你必须掌握这项技术。

负责从用户那里获取名称的视图控制器需要有一种方法来告诉它的主视图控制器用户输入了一个名称。这是通过授权实现的。

你应该做什么:

基本上,您创建一个名为“NamePrompterViewControllerDelegate”之类的协议,该协议将至少有一个方法,当用户完成输入他的名字时将调用该方法。

您的 ScoreKeepViewController 应该有一个实现协议的实例变量(请查看有关协议的苹果文档以获得帮助)

您的主视图控制器(包含您添加的方法的那个)然后应该实现您创建的协议,并将自己设置为 ScoreKeep 的委托,如下所示:

stats = [[ScoreKeep alloc] initWithNibName:@"Scorekeep" bundle:nil];
stats.delegate = self;

有关以模态方式呈现和关闭 ViewController 的更多信息,您应该阅读Apple 文档中的文档

我希望我对您有所帮助,要涵盖的内容很多,而且很难通过写答案来完成。

随时要求更多许可。

于 2011-01-10T00:12:20.530 回答