如何重置viewcontroller的值?

时间:2012-03-28 23:07:47

标签: objective-c ios xcode uiviewcontroller

当我回到主窗口时,如何重置应用程序的值? 我有一个应用程序惠特多个视图,最后一个窗口包含一个计时器和2个计数器,我第一次运行应用程序它很好,但当我回到第一个窗口并按照相同的步骤进入最后一个窗口,第一次价值仍在那里。我该怎么办?

这是代码(最终窗口):

    `enter code here`
     // Created by MiniMac on 3/26/12.
     // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
     //

     #import "juegoAB.h"
     #import "SwitchViewController.h"

     @implementation juegoAB
     @synthesize cronometro, button, Rounds;

      int y = 0;

      - (IBAction)buttonPressed: (id) sender {
          y++;
          if (y>10) y=10;
              [self updateText];
      }

      - (void)updateText {
          switch (y) {
              case 0: [Rounds setText:@"5."]; break;
              case 1: [Rounds setText:@"5."]; break;
              case 2: [Rounds setText:@"4."]; break;
              case 3: [Rounds setText:@"4."]; break;
              case 4: [Rounds setText:@"3."]; break;
              case 5: [Rounds setText:@"3."]; break;
              case 6: [Rounds setText:@"2."]; break;
              case 7: [Rounds setText:@"2."]; break;
              case 8: [Rounds setText:@"1."]; break;
              case 9: [Rounds setText:@"1."]; break;
              case 10: [Rounds setText:@"0."]; break;
              default: break;
          }
      }

      NSTimer *timer; int i;

      -(IBAction)playsound {
          NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:                            
             @"%@/disparo04.WAV",[[NSBundle mainBundle] resourcePath]]];                                                                                       

          NSError *error;
          audioplayer = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:&error];
          audioplayer.numberOfLoops = 0;
          if(audioplayer == nil) NSLog(@"%@",[error description]);
          else [audioplayer play];
      }

      -(IBAction)pressButton {
          i = 11;
          timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                   target:self
                                                 selector:@selector(timerFires)  
                                                 userInfo:nil
                                                  repeats:YES];
          [timer fire];
      }

      - (void) timerFires{
          if(i > 0) {
              i--;
              cronometro.text = [NSString stringWithFormat:@"%i", i];
          } else {
              cronometro.text = @"TIEMPO!!";
              timer = nil;
          }
      }

      - (IBAction)onStopPressed: (id)sender {
          [timer invalidate];
          timer = nil;
      }

      -(IBAction)generateNumbers {
          int randomNumber = rand() % 26;
          switch (randomNumber) {
              case 0:
                  label.text = @"A";
                  label1.text = @"Z";
                  label2.text = @"Q";
                  break;
              case 1:
                  label.text = @"B";
                  label1.text = @"Y";
                  label2.text = @"W";
                  break;

      -(IBAction)plus {
          counter=counter + 1;
          count.text = [NSString stringWithFormat:@"%i",counter];
      }

      -(IBAction)zero {
          counter=counter - 1;
          count.text = [NSString stringWithFormat:@"%i",counter];
      }

      -(IBAction)plus2 {
          counter2=counter2 + 1;
          count2.text = [NSString stringWithFormat:@"%i",counter2];
      }

      -(IBAction)zero2 {
          counter2=counter2 - 1;
          count2.text = [NSString stringWithFormat:@"%i",counter2];
      }

      - (void)viewWillAppearBOOL)animated {
          [super viewWillAppear:animated];
      }

      - (id)initWithNibName:(NSString *)nibNameOrNil 
             bundleNSBundle:(NSString *)nibBundleOrNil {                                                                                          
          self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
          if (self) {
              // Custom initialization
          }
          return self;
      }

      - (IBAction)goBack {
          SwitchViewController *second = [[SwitchViewController alloc]    
                                           initWithNibName:nil bundle:nil];
          [self presentModalViewController:second animated:YES];
          [second release];
      }

      - (void)dealloc {
          [cronometro release];
          [button release];
          [Rounds release];
          [super dealloc];
      }

      - (void)didReceiveMemoryWarning {
          // Releases the view if it doesn't have a superview.
          [super didReceiveMemoryWarning];

          // Release any cached data, images, etc that aren't in use.
      }

      #pragma mark - View lifecycle

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

      - (void)viewDidUnload {
          [super viewDidUnload];
          // Release any retained subviews of the main view.
          // e.g. self.myOutlet = nil;
      }

      - (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation {                           
           // Return YES for supported orientations
           return (interfaceOrientation == UIInterfaceOrientationPortrait);
       }

       - (void)resetToOriginalState: (id)sender {
           //Do your stuff here 
       }

       @end

有一种方法可以回到我的第一个窗口并重置应用程序,就像第一次打开它一样。

我希望有人能帮忙...... 感谢名单!

1 个答案:

答案 0 :(得分:0)

更改viewWillAppear以重置您的值(如下所示:)

    - (void)viewWillAppearBOOL)animated {. 
        [super viewWillAppear:animated];
        counter = 0;
        counter2 = 0;
        timer = 0;
    }