سؤال

I have a function here that upon completing a single round, if your score is higher than either a default score entry or a newly placed high score then it will swap its data with your data and push everything else down. removing the last entry from the list. currently this is just one exchange and for functions sake I'm going to hard code it and then refactor it later.

My main problem is that when I set up a text input view to capture the players name execution continues immediately without the players input and crashes the game. I commented out the line that sets the text because I have a default value in place just in case any attempt that I try to make fails. How can I get Execution to wait for a moment while input is taken? Would I have to set up a delegate method? If so I'm still a bit confused by delegates. I could set it up to work but I don't understand it, so I wouldn't be able to do any other special custom tasks with it. I've worked on it for a while and got no further...

-(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];

}

Help would greatly be appreciated. If more information is needed I'd be happy to provide. by the way ScoreKeep is an object that contains a dictionary and a function to create a dictionary such that it can set any values I need and package them into sComponents(the dictionary to be entered into the main savefile)

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

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

}

I'm going to try making a utility file that works independently of the app so that I Can update files and perform other universal operations such as saving when needed. Its a step in a direction that i'd like to take.

هل كانت مفيدة؟

المحلول

If i get it right, (The code is really nasty, man...) your problem is that you are trying to present a View Controller with the wrong way.

Correct me if i'm wrong, is ScoreKeep is a ViewController? if so, you have to name it properly. that's for a start.

Second, you cant present another view controller only by adding its "view" property to the current view controller's View Hierarchy. that way the view will not respond properly to the events.

the correct way to to what you'r trying to do is by presenting the ScoreKeep ViewController modally.

there is no other right way to do this without using delegation. you will have to acquire this technique.

Your view controller that responsible for getting the name from the user need to have a way to tell it's master view controller that the user entered a name. and that is achieved through delegation.

What you should do:

Basically you create a protocol called something like "NamePrompterViewControllerDelegate" that will have at least one method that will be called when the user will done entering his name.

Your ScoreKeepViewController should have an instance variable that implemented the protocol (Look at the apple documentation on protocols for assistance)

Your main view controller (the one that contains the method you added) then should implement the protocol you created, and set itself as the delegate of ScoreKeep like that:

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

For more info on presenting and dismissing ViewControllers modally you should read the documentation at Apple Documentation

I hope i helped you, there is just a lot to cover and it hardly can be done by writing an answer.

Feel free to ask more for clearance.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top