我知道这个问题被问了很多,但我是 Objective-C 的新手。我在第一个 viewcontroller.h 中的代码:
@interface ViewController : UIViewController
@property (nonatomic) int mutecounter;
@property (nonatomic) ViewController *targetLevelController;
@end
我在 .m 文件的第一个视图控制器中的代码:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize mutecounter;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *musicFile = [[NSBundle mainBundle] URLForResource:@"Ambler" withExtension:@"mp3"];
self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
self.backgroundMusic.numberOfLoops = -1;
self.backgroundMusic.volume = 0.3;
[self.backgroundMusic play];
[self.muteButtonOutlet setBackgroundImage:[UIImage imageNamed:@"mutebutton.png"] forState: UIControlStateNormal];
_targetLevelController=[[ViewController alloc]init];
_targetLevelController.mutecounter = mutecounter;
}
- (IBAction)muteButton:(id)sender {
if (mutecounter % 2 == 0){
self.backgroundMusic.volume = 0;
mutecounter = mutecounter + 1;
[self.muteButtonOutlet setBackgroundImage:[UIImage imageNamed:@"unmutebutton.png"] forState: UIControlStateNormal];
} else {
self.backgroundMusic.volume = 0.3;
mutecounter = mutecounter + 1;
[self.muteButtonOutlet setBackgroundImage:[UIImage imageNamed:@"mutebutton.png"] forState: UIControlStateNormal];
}
}
我在第二个 Viewcontroller.h 中的代码:
@interface LevelController : UIViewController
- (IBAction)muteButton:(id)sender;
@end
和我在第二个 Viewcontroller.m 中的代码
#import "LevelController.h"
#import "ViewController.h"
@interface LevelController ()
@end
@implementation LevelController
int mutecounter2;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *musicFile = [[NSBundle mainBundle] URLForResource:@"Ambler" withExtension:@"mp3"];
self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
self.backgroundMusic.numberOfLoops = -1;
self.backgroundMusic.volume = 0.3;
[self.backgroundMusic play];
ViewController *targetLevelController=[[ViewController alloc]init];
mutecounter2 = targetLevelController.mutecounter;
if (mutecounter2 % 2 == 0){
self.backgroundMusic.volume = 0;
[self.muteButtonOutlet setBackgroundImage:[UIImage imageNamed:@"mutebutton.png"] forState: UIControlStateNormal];
} else {
self.backgroundMusic.volume = 0.3;
[self.muteButtonOutlet setBackgroundImage:[UIImage imageNamed:@"unmutebutton.png"] forState: UIControlStateNormal];
}
}
- (IBAction)muteButton:(id)sender {
if (mutecounter2 % 2 == 0){
self.backgroundMusic.volume = 0;
mutecounter2 = mutecounter2 + 1;
[self.muteButtonOutlet setBackgroundImage:[UIImage imageNamed:@"unmutebutton.png"] forState: UIControlStateNormal];
} else {
self.backgroundMusic.volume = 0.3;
mutecounter2 = mutecounter2 + 1;
[self.muteButtonOutlet setBackgroundImage:[UIImage imageNamed:@"mutebutton.png"] forState: UIControlStateNormal];
}
}
问题是代码不起作用。有人可以帮助我吗?我总是在 mutecounter2 中得到值 0。但它应该是 1 或 2 或 3 之类的。谢谢马蒂斯