在iOS应用程序中的视图控制器之间传递BOOL |将对象传递给iOS应用程序中的视图控制器

在iOS应用程序中的视图控制器之间传递BOOL |将对象传递给iOS应用程序中的视图控制器,ios,objective-c,boolean,Ios,Objective C,Boolean,我试图将BOOL值从另一个视图控制器(OptionsViewController)传递给视图控制器(GameViewController),但什么也没发生 我在GameViewController.h中为BOOL创建了一个属性 @property (assign, nonatomic) BOOL isMusicEnabled; 我告诉OptionViewController关于GameViewController的事 #import "GameViewController.h" 然后按一次按

我试图将BOOL值从另一个视图控制器(OptionsViewController)传递给视图控制器(GameViewController),但什么也没发生

我在GameViewController.h中为BOOL创建了一个属性

@property (assign, nonatomic) BOOL isMusicEnabled;
我告诉OptionViewController关于GameViewController的事

#import "GameViewController.h"
然后按一次按钮将在GameViewController中将isMusicEnabled设置为BOOL值YES,再次按一次按钮将isMusicEnabled设置为NO

但看起来不像是

//GameViewController
if(isMusicEnabled == YES){
      NSLog(@"isMusicEnabled == YES");
}
if(isMusicEnabled == NO){
      NSLog(@"isMusicEnabled == NO");
}
正在访问

//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];
}

我试过使用segue,但只要我使用标识符并推送视图,它就可以正常工作,但这次我只想在不推送视图的情况下传递BOOL值。

要在以前的视图控制器中获取变量的更新值,可以尝试的一种解决方案是使用
NSUserDefaults
。使用此选项,您可以在第一视图控制器中设置初始值。在您的secondViewController中执行某些操作后使用新值更新
NSUserDefaults
变量。

没有将代码从
选项ViewController
移动到
GameViewController
,您从未推送过gameViewController实例。请检查此ans@AntonyRaphel-已阅读该文章,但它不起作用:(