2017-02-16 10 views
0

他のView Controller(OptionsViewController)からBOOL値をView Controller(GameViewController)に転送しようとしていますが、何も起こりません。iOSアプリケーションのView Controller間でBOOLを渡す|

私はGameViewViewController.h

@property (assign, nonatomic) BOOL isMusicEnabled; 

にBOOLのプロパティを作成し、私はGameViewController

#import "GameViewController.h" 

についてOptionsViewControllerを告げた後、一度ボタンを押すと、その後にGameViewControllerにisMusicEnabledを設定しますBOOLの値がYESで、もう一度押すとisMusicEnabledがNOに設定されます。

しかし

//GameViewController 
if(isMusicEnabled == YES){ 
     NSLog(@"isMusicEnabled == YES"); 
} 
if(isMusicEnabled == NO){ 
     NSLog(@"isMusicEnabled == NO"); 
} 

がアクセスされているようにそれはdoesn'tようです。

//OptionsViewController 
- (void) MusicRingPushed:(id)sender 
{ 
    NSLog(@"MusicRingPushed:(id)sender."); 
    //GameViewController *gameViewController = [[GameViewController alloc] initWithNib:@"GameViewController" bundle:nil]; 
    GameViewController *gameViewController = [[GameViewController alloc] initWithNibName:@"GameViewController" bundle:nil]; 

    if(_MusicON == YES){ 
     [_MusicRing setImage:[UIImage imageNamed:@"ring2hd.png"] forState:UIControlStateNormal]; 
     [self.view addSubview:_MusicRing]; 
     _MusicON = NO; 
     NSLog(@"_MusicON ==YES"); 
     gameViewController.isMusicEnabled = NO; 
     //[self pushViewController:gameViewController animated:YES]; //doesn´t work 
    } 
    else if (_MusicON == NO){ 
     [_MusicRing setImage:[UIImage imageNamed:@"ring1hd.png"] forState:UIControlStateNormal]; 
     [self.view addSubview:_MusicRing]; 
     _MusicON = YES; 
     NSLog(@"_MusicON == NO"); 
     gameViewController.isMusicEnabled = YES; 
     //[self pushViewController:gameViewController animated:YES]; //doesn´t work 
     } 
} 

//GameViewController 
- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 

    // Configure the view. 
    SKView *skView = (SKView *)self.view; 
    skView.multipleTouchEnabled = NO; 

    // Create and configure the scene. 
    self.scene = [GameScene sceneWithSize:skView.bounds.size]; 
    self.scene.scaleMode = SKSceneScaleModeAspectFill; 

    // Load the level. 
    self.level = [[Level alloc] init]; 
    self.scene.level = self.level; 

    //creates the block and assigns it to GameScene’s swipeHandler property. 
    /*id block = ^(Swap *swap) { 
     self.view.userInteractionEnabled = NO; 

     if ([self.level isPossibleSwap:swap]) { 
      NSLog(@"*** [self.level isPossibleSwap:swap];"); 
      [self.level performSwap:swap]; 
      [self.scene animateSwap:swap completion:^{ 
       //self.view.userInteractionEnabled = YES; 
       [self handleMatches]; 
      }]; 
     } else { 
       self.view.userInteractionEnabled = YES; 
      } 
     }; 

     self.scene.swipeHandler = block;*/ 

     id block = ^(Swap *swap) { 
      self.view.userInteractionEnabled = NO; 
      NSLog(@"id block = ^(Swap *swap)"); 
      [self.level performSwap:swap]; 
      [self.scene animateSwap:swap completion:^{ 
       NSLog(@"[self.scene animateSwap:swap completion:"); 
       [self handleMatches]; 
       self.view.userInteractionEnabled = YES; 
      }]; 
     }; 

     self.scene.swipeHandler = block; 

     // Present the scene. 
     [skView presentScene:self.scene]; 

     //loads the background music MP3 and sets it to loop forever 
     NSURL *url = [[NSBundle mainBundle] URLForResource:@"Mining by Moonlight" withExtension:@"mp3"]; 
     self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil]; 
     self.backgroundMusic.numberOfLoops = -1; 
     [self.backgroundMusic play]; 

     if(isMusicEnabled == YES){ 
      NSLog(@"isMusicEnabled == YES"); 
     } 
     if(isMusicEnabled == NO){ 
      NSLog(@"isMusicEnabled == NO"); 
     } 
     if(isSFXEnabled == YES){ 
      NSLog(@"isSFXEnabled == YES"); 
     } 
     if(isSFXEnabled == NO){ 
      NSLog(@"isSFXEnabled == NO"); 
     } 
     // Let's start the game! 
     [self beginGame]; 
} 

I'veはセグエにしようと、それは限り、私は識別子を使用してビューを押すと、正常に動作しますが、今回は私はちょうどビューを押さずBOOL値を渡したいです。

+0

'OptionsViewController'から' GameViewController'への移動コードはありません。あなたは決してそのgameViewControllerインスタンスをプッシュしません。 –

+0

これをチェックしてくださいhttp://stackoverflow.com/a/9736559/4831524 –

+0

@AntonyRaphel - すでに記事を読んで、うまくいきませんでした。 :( – Chris79

答えて

0

以前のビューコントローラで変数の更新値を取得するには、試してみることのできる解決策の1つがNSUserDefaultsです。これを使用して、firstViewControllerに初期値を設定できます。そして、のsecondViewControllerで実行されたアクションの後に、NSUserDefaults変数を新しい値で更新します。

関連する問題