1

我用 xcode 4 制作了一个应用程序,所有代码都在主代码文件中,所以我没有更多的类。我有 3 个视图,但目前只使用一个。现在在游戏中我保存一个整数作为分数,在游戏结束时它显示你的分数是多少,但我想知道如何让该整数显示在我的其他 2 个视图之一的标签中,并且重新启动应用程序时如何将其保留在那里。我很抱歉我的英语不好,但我是这个目标 C 和 iphone 应用程序的新手。谢谢你!

以及如何将某些内容放入其他视图控制器中,无法处理代码文件

这是代码:

//
//  ViewController.m
//  Mundo Grafia
//
//  Created by Axel Lambregts on 2/10/12.
//  Copyright (c) 2012 AxelNiels. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end
@implementation ViewController


// variabelen aanmaken
NSInteger intScore;
NSInteger intRandomMonument;
NSInteger intRandomAnswerPosition;
NSInteger intRandomAnswerA;
NSInteger intRandomAnswerB;
NSInteger intRandomAnswerC;
NSInteger intRandomAnswerD;
NSInteger intArrayEuropeLenght;
NSInteger intAmountOfQuestionsAsked;
NSString *scoreSpatie;
NSString *arrEurope[][2] =

    {

        {@"Eifel.jpg",@"Paris"},
        {@"NotreDame.jpg",@"Paris"},
        {@"ArcDeTriompheParis.jpg",@"Paris"},

        {@"AtomuimBrussels.jpg",@"Brussels"},
        {@"MannekePis.jpg",@"Brussels"},

        {@"Colosseum.jpg",@"Rome"},
        {@"TreviFountainRome.jpg",@"Rome"},

        {@"SagradaFamilia.jpg",@"Barcelona"},
        {@"SpanishSchoolMadrid.jpg",@"Madrid"},

        {@"KremlinMoskou.jpg",@"Moskou"},

        {@"MermaidKopenhagen.jpg",@"Copenhagen"},

        {@"MonteCarloMonaco.jpg",@"Monaco"},

        {@"AcropolisAthens.jpg",@"Athens"},

        {@"ParlementBudapest.jpg",@"Budapest"},

        {@"BuildingReykjavik.jpg",@"Reykjavik"},

        {@"StatueKiev.jpg",@"Kiev"},

        {@"PortOfEuropeLissabon.jpg",@"Lisbon"},

        {@"ViewSanMarino.jpg",@"San Marino"},

        {@"BridgesPrague.jpg",@"Prague"},

        {@"MoskeeAnkara.jpg",@"Ankara"},

        {@"TowerBridgeLondon.jpg",@"London"},
        {@"BigBenLondon.jpg",@"London"},
        {@"LondonEyeLondon.jpg",@"London"},

        {@"BrandenburgerTor.jpg",@"Berlin"},
        {@"ReichstagBerlin.jpg",@"Berlin"},

        {nil,nil}

    };


/*
NSArray *arrEurope = @[
@[@"Eifel.jpg",@"Paris"],
@[@"NotreDame.jpg",@"Paris"],
@[@"ArcDeTriompheParis.jpg",@"Paris"],
@[@"Eifel.jpg",@"Paris"],
@[@"NotreDame.jpg",@"Paris"],
@[@"ArcDeTriompheParis.jpg",@"Paris"],

@[@"AtomuimBrussels.jpg",@"Brussels"],
@[@"MannekePis.jpg",@"Brussels"],

@[@"Colosseum.jpg",@"Rome"],
@[@"TreviFountainRome.jpg",@"Rome"],

@[@"SagradaFamilia.jpg",@"Barcelona"],
@[@"SpanishSchoolMadrid.jpg",@"Madrid"],

@[@"KremlinMoskou.jpg",@"Moskou"],

@[@"MermaidKopenhagen.jpg",@"Copenhagen"],

@[@"MonteCarloMonaco.jpg",@"Monaco"],

@[@"AcropolisAthens.jpg",@"Athens"],

@[@"ParlementBudapest.jpg",@"Budapest"],

@[@"BuildingReykjavik.jpg",@"Reykjavik"],

@[@"StatueKiev.jpg",@"Kiev"],

@[@"PortOfEuropeLissabon.jpg",@"Lisbon"],

@[@"ViewSanMarino.jpg",@"San Marino"],

@[@"BridgesPrague.jpg",@"Prague"],

@[@"MoskeeAnkara.jpg",@"Ankara"],

@[@"TowerBridgeLondon.jpg",@"London"],
@[@"BigBenLondon.jpg",@"London"],
@[@"LondonEyeLondon.jpg",@"London"],

@[@"BrandenburgerTor.jpg",@"Berlin"],
@[@"ReichstagBerlin.jpg",@"Berlin"]



];


*/



//methode'start' wanneer er op de start-knop wordt gedrukt
-(IBAction)start{



    [self NewQuestionEurope];
    intAmountOfQuestionsAsked = 0;
    //reset and show score
    intScore = 0;
    score.text = [NSString stringWithFormat:@"%d", intScore];

    //make buttons and labels visible
    Start.hidden = YES;
    question.hidden = NO;
    score.hidden = NO;
    scoretext.hidden = NO;
    imageContainer.hidden = NO;


    answerA.hidden = NO;
    answerB.hidden = NO;
    answerC.hidden = NO;
    answerD.hidden = NO;

    answerA.textColor = [UIColor greenColor];
    answerB.textColor = [UIColor greenColor];
    answerC.textColor = [UIColor greenColor];
    answerD.textColor = [UIColor greenColor];


    clickButtonA.hidden = NO;
    clickButtonB.hidden = NO;
    clickButtonC.hidden = NO;
    clickButtonD.hidden = NO;

    GoodFalse.hidden = NO;
    GoodFalse.text=@"Press a button to answer";
    GoodFalse.textColor = [UIColor greenColor];
   question.text = @"Where is this picture taken?";

}//einde start





-(IBAction)Reset{

score.text= @"gelukt";



}


-(IBAction)NewQuestionEurope{


  if(intAmountOfQuestionsAsked == 19){

      question.text = [@"Uw score is: "stringByAppendingString: [NSString stringWithFormat:@"%d", intScore]];

      Start.hidden = NO;
      question.hidden = NO;
      score.hidden = YES;
      scoretext.hidden = YES;
      imageContainer.hidden = YES;


      answerA.hidden = YES;
      answerB.hidden = YES;
      answerC.hidden = YES;
      answerD.hidden = YES;

      clickButtonA.hidden = YES;
      clickButtonB.hidden = YES;
      clickButtonC.hidden = YES;
      clickButtonD.hidden = YES;
      GoodFalse.hidden = YES;

      Start.titleLabel.text = @"END";



  }

    else{




    intAmountOfQuestionsAsked ++;



    //Deze lus geeft aan intArrayEuropeLenght de waarde van hoeveel rijen de array bevat//
    intArrayEuropeLenght = 0;

    while (arrEurope[intArrayEuropeLenght][0] != NULL)
    {
        intArrayEuropeLenght++;
    }


   //score.text = [NSString stringWithFormat:@"%d", intScore];


    scoreSpatie = [
                             [NSString stringWithFormat:@"%d", intScore]
                             stringByAppendingString:@"/"];
    score.text = [
                  scoreSpatie
                  stringByAppendingString:[NSString stringWithFormat:@"%d", intAmountOfQuestionsAsked]
                  ];



  // Randoms genereren voor: 4 random antwoorden; 1 random afbeelding; een random positie om het juiste antwoord in te plaatsen
    intRandomMonument = arc4random()%intArrayEuropeLenght;            
    intRandomAnswerA = arc4random()%intArrayEuropeLenght;
    intRandomAnswerB = arc4random()%intArrayEuropeLenght;
    intRandomAnswerC = arc4random()%intArrayEuropeLenght;
    intRandomAnswerD = arc4random()%intArrayEuropeLenght;
    intRandomAnswerPosition = arc4random()%4;



    while(arrEurope[intRandomMonument][0] == @"removed"){

        intRandomMonument = arc4random()%intArrayEuropeLenght; 

    }


  //Random antwoorden maken
    answerA.text = @"";
    answerB.text = @"";
    answerC.text = @"";
    answerD.text = @"";

    if(answerA.text = @""){
        answerA.text = arrEurope[intRandomAnswerA][1];
    }

    if(answerB.text = @""){
        answerB.text = arrEurope[intRandomAnswerB][1];
    }

    if(answerC.text = @""){
        answerC.text = arrEurope[intRandomAnswerC][1];
    }

    if(answerD.text = @""){
        answerD.text = arrEurope[intRandomAnswerD][1];
    }



    switch(intRandomAnswerPosition)
    {
        case 0: answerA.text = arrEurope[intRandomMonument][1];
            break;
        case 1: answerB.text = arrEurope[intRandomMonument][1];
            break;
        case 2: answerC.text = arrEurope[intRandomMonument][1];
            break;
        case 3: answerD.text = arrEurope[intRandomMonument][1];
    };



    //als er antwoorden hetzelfde zijn, voor de gelijke een random blijven maken tot ze niet hetzelfde zijn
    if(
       [answerA.text isEqualToString: answerB.text]||
       [answerA.text isEqualToString: answerC.text]||
       [answerA.text isEqualToString: answerD.text]
       ){

        answerA.text = arrEurope[arc4random()%intArrayEuropeLenght][1];

    };

    if(
       [answerB.text isEqualToString: answerA.text]||
       [answerB.text isEqualToString: answerC.text]||
       [answerB.text isEqualToString: answerD.text]
       ){

        answerB.text = arrEurope[arc4random()%intArrayEuropeLenght][1];

    };

    if(
       [answerC.text isEqualToString: answerA.text]||
       [answerC.text isEqualToString: answerB.text]||
       [answerC.text isEqualToString: answerD.text]
       ){

        answerC.text = arrEurope[arc4random()%intArrayEuropeLenght][1];

    };

    if(
       [answerD.text isEqualToString: answerA.text]||
       [answerD.text isEqualToString: answerB.text]||
       [answerD.text isEqualToString: answerC.text]
       ){

        answerD.text = arrEurope[arc4random()%intArrayEuropeLenght][1];

    };




    //Laat de afbeelding zien van het gekozen monument (via random gekozen)
    imageContainer.image = [UIImage imageNamed: arrEurope[intRandomMonument][0]];




  }  

}


//hier onder zit ergens een fout want hij geeft precies random juist en fout :)
//kan zijn dat het opgelost is vanaf we niet 2x dezelfde vragen hebben maar ik denk het niet.

//press buttonA
-(IBAction)AnswerA {



    if([answerA.text isEqualToString: arrEurope[intRandomMonument][1]]){

        GoodFalse.text = @"Correct!";
        GoodFalse.textColor = [UIColor greenColor];
        intScore ++;

    }else {

        GoodFalse.text=@"Wrong!";
        GoodFalse.textColor = [UIColor redColor];

        }

    arrEurope[intRandomMonument][0] = @"removed";

    [self NewQuestionEurope];

}

//press buttonB
-(IBAction)AnswerB {

    if([answerB.text isEqualToString: arrEurope[intRandomMonument][1]]){

        GoodFalse.text = @"Correct!";
        GoodFalse.textColor = [UIColor greenColor];
        intScore ++;

    }else {

        GoodFalse.text=@"Wrong!";
        GoodFalse.textColor = [UIColor redColor];
    }

    arrEurope[intRandomMonument][0] = @"removed";
[self NewQuestionEurope];
}

//press buttonC (works)
-(IBAction)AnswerC {


    if([answerC.text isEqualToString: arrEurope[intRandomMonument][1]]){

        GoodFalse.text = @"Correct!";
        GoodFalse.textColor = [UIColor greenColor];
        intScore ++;

    }else {

        GoodFalse.text=@"Wrong!";
        GoodFalse.textColor = [UIColor redColor];
    }

arrEurope[intRandomMonument][0] = @"removed";
    [self NewQuestionEurope];
}


//press buttonD
-(IBAction)AnswerD {


    if([answerD.text isEqualToString: arrEurope[intRandomMonument][1]]){

        GoodFalse.text = @"Correct";
        GoodFalse.textColor = [UIColor greenColor];
        intScore ++;

    }else {

        GoodFalse.text=@"Wrong!";
        GoodFalse.textColor = [UIColor redColor];
    }
   arrEurope[intRandomMonument][0] = @"removed";
    [self NewQuestionEurope];
}





//hier onder niets wijzigen


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.




}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
4

4 回答 4

3
NSUserDefaults *scores = [NSUserDefaults standardUserDefaults];
if (_points>[scores integerForKey :@"highscore"]) {
    [_highScoresLabel setString:[NSString stringWithFormat:@"%d",_points]];
    [scores setInteger:_points forKey:@"highscore"];

我正在使用该代码在我的一个游戏中显示持续的高分。

要在另一个 UIViewController 中显示,只需添加以下代码,其中 _higScoreLabel 是 ypu 想要显示分数的标签:

NSUserDefaults *scores = [NSUserDefaults standardUserDefaults];
NSString *_points = [scores integerForKey :@"highscore"];
[_highScoresLabel setString:[NSString stringWithFormat:@"%d",_points]];
于 2012-10-16T17:59:04.533 回答
0
Try using NSUserDefaults ,it will keep the value in memory until you forcefully remove it or you delete the app.

     int Score = HighScore;

    [[NSUserDefaults standardUserDefaults] setObject:Score forKey:@"anyKey"];

    currentScoreLabel.text = [NSString stringWithFormat:@"%d",Score];
    highScoreLabel.text=[NSString stringWithFormat:@"%d",[[NSUserDefaults standardUserDefaults] objectForKey:@"anyKey"]];
于 2012-10-16T18:39:26.200 回答
0

一旦你知道用户的最终分数是多少,就将整数写入 NSUserDefaults,如下所示:

[[NSUserDefaults standardUserDefaults] setInteger:intScore forKey:@"score"];

然后,为了在另一个视图控制器中显示分数整数,您只需在另一个视图控制器的 viewDidLoad 中从磁盘读取值,然后将其设置在标签中!由于该值已经存储在磁盘上,因此您甚至不需要将其作为参数传递或将其设置为另一个视图控制器上的属性,只需从 viewDidLoad 中的 NSUserDefaults 读取它(或您需要设置的任何/任何方法) )。

所以在第二个视图控制器中,做:

- (void) viewDidLoad {
    self.anotherLabel.text = [NSString stringWithFormat:@"%d", [defaults integerForKey:@"score"]];
}
于 2012-10-16T18:03:38.317 回答
0

// 保存在首选项中

int highScore  = yourGameScore;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore]     forKey:@"HighScore"];
[[NSUserDefaults standardUserDefaults] synchronize];

// 从 pref 中获取保存的值

int highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] intValue ];

这是堆栈中的类似问题..

于 2012-10-16T18:13:02.873 回答