NSTimer を使用して毎秒サウンドを再生する簡単なプログラムがあります。NSTimer 変数を宣言しましたが、XCode はそれが未使用であると認識しています。これに加えて、タイマーは無効化コマンドに応答せず、解放したり nil に設定したりすることさえしません。行の可変部分を取り除き、「[NSTimer ...]」部分だけを取り除こうとしましたが、後で無効にすることはできません。
.h ファイルは次のとおりです。
#import <UIKit/UIKit.h>
@interface timer_workingViewController : UIViewController {
NSTimer *timer1;
}
@property (nonatomic, retain) NSTimer *timer1;
- (IBAction)startButtonPressed:(id)sender;
- (IBAction)stopButtonPressed:(id)sender;
- (void)timerFunction:(id)sender;
@end
.m ファイルは次のとおりです。
#import "timer_workingViewController.h"
#import <AudioToolbox/AudioToolbox.h>
@implementation timer_workingViewController
@synthesize timer1;
- (IBAction)startButtonPressed:(id)sender {
NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(timerFunction:) userInfo:nil repeats: YES];
}
- (IBAction)stopButtonPressed:(id)sender {
[timer1 invalidate];
[timer1 release];
timer1 = nil;
}
- (void)timerFunction:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"bell" ofType:@"wav"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &soundID);
AudioServicesPlaySystemSound(soundID);
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
self.timer1 = nil;
}
- (void)dealloc {
[timer1 invalidate];
[super dealloc];
}
@end
nib ファイルは、開始ボタンと停止ボタンで構成されています。開始ボタンを押すと、タイマーが開始され、ファイルが完全に正常に再生されますが、開始すると停止することはできません。
ここで何か明らかに間違っていますか?オンライン検索では何も見つかりませんでした。私が試したものはまったく機能しません。