我试图将BOOL值从另一个视图控制器(OptionsViewController)传递给视图控制器(GameViewController),但没有任何反应。
我在GameViewViewController.h中为BOOL创建了一个属性
@property (assign, nonatomic) BOOL isMusicEnabled;
我告诉OptionsViewController关于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值而不推送视图。
答案 0 :(得分:0)
要在先前的视图控制器中获取变量的更新值,您可以尝试使用NSUserDefaults
的一个解决方案。使用此功能,您可以在 firstViewController 中设置初始值。在 secondViewController 中执行某些操作后,使用新值更新NSUserDefaults
变量。