0

I´m trying to pass a BOOL value forward to a view controller (GameViewController) from another view controller (OptionsViewController), but nothing happens.

I created a property for the BOOL in in GameViewViewController.h

@property (assign, nonatomic) BOOL isMusicEnabled;

and I told OptionsViewController about the GameViewController

#import "GameViewController.h"

and then pressing a button once would then set isMusicEnabled in GameViewController to BOOL value YES and pressing it again would set isMusicEnabled to NO;

But it doesn´t seem like

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

is being accessed.

//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 tried with a segue and then it works fine, as long as I use an identifier and push a view, but this time I just want to pass a BOOL value without pushing the view.

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Chris79
  • 33
  • 7

1 Answers1

0

To get the updated value of a variable in your previous view controller then one solution you can try is use NSUserDefaults. Using this you can set initial value in your firstViewController. And after some action performed in your secondViewController update the NSUserDefaults variable with the new value.

halfer
  • 19,824
  • 17
  • 99
  • 186
Er. Vihar
  • 1,495
  • 1
  • 15
  • 29