iPhone:NSTimer赢得';t响应invalidate,XCode坚持该变量未声明
我有一个简单的程序,它使用NSTimer每秒播放一个声音。我已经声明了NSTimer变量,但XCode认为它未使用。除此之外,计时器不会响应invalidate命令,甚至不会释放该命令或将其设置为nil。我已经试着去掉行的可变部分,只使用“[NSTimer…]”部分,但是以后我无法使它无效 以下是.h文件:iPhone:NSTimer赢得';t响应invalidate,XCode坚持该变量未声明,iphone,xcode,nstimer,Iphone,Xcode,Nstimer,我有一个简单的程序,它使用NSTimer每秒播放一个声音。我已经声明了NSTimer变量,但XCode认为它未使用。除此之外,计时器不会响应invalidate命令,甚至不会释放该命令或将其设置为nil。我已经试着去掉行的可变部分,只使用“[NSTimer…]”部分,但是以后我无法使它无效 以下是.h文件: #import <UIKit/UIKit.h> @interface timer_workingViewController : UIViewController {
#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
#导入
@接口计时器\u工作视图控制器:UIViewController{
n定时器*定时器1;
}
@属性(非原子,保留)NSTimer*timer1;
-(iAction)启动按钮按下:(id)发送方;
-(iAction)按下停止按钮:(id)发送方;
-(void)timer函数:(id)发送方;
@结束
下面是.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
#导入“timer\u workingViewController.h”
#进口
@实现计时器\u工作视图控制器
@合成定时器1;
-(iAction)启动按钮按下:(id)发送方{
NSTimer*timer1=[NSTimer scheduledTimerWithTimeInterval:1.0目标:自选择器:@selector(timerFunction:)用户信息:无重复:是];
}
-(iAction)停止按钮按下:(id)发件人{
[计时器1失效];
[计时器1释放];
定时器1=零;
}
-(void)timer函数:(id)发送方{
NSString*path=[[NSBundle mainBundle]pathForResource:@“bell”类型:@“wav”];
系统声音ID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path],&soundID);
AudioServicesPlaySystemSound(soundID);
}
-(无效)viewDidLoad{
[超级视图下载];
}
-(无效)未收到记忆警告{
[超级记忆警告];
}
-(无效)视图卸载{
self.timer1=nil;
}
-(无效)解除锁定{
[计时器1失效];
[super dealoc];
}
@结束
nib文件由开始按钮和停止按钮组成。按下“开始”按钮会使计时器启动,文件播放得非常好,但一旦启动就无法停止
这里有什么明显的问题吗?在线搜索没有找到任何结果,而我尝试的任何结果都不起作用。您通过在startButtonPressed中声明同名的局部变量来隐藏timer1的成员声明: 删除NSTimer*声明,以便将新计时器分配给成员变量。您还需要执行retain操作,以便您的成员变量保留引用
- (IBAction)startButtonPressed:(id)sender {
timer1 = [[NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(timerFunction:) userInfo:nil repeats: YES] retain];
}
另外,请确保释放timer1,并在使用完毕后将其设置为nil。您通过在startButtonPressed中声明一个同名的局部变量来隐藏timer1的成员声明: 删除NSTimer*声明,以便将新计时器分配给成员变量。您还需要执行retain操作,以便您的成员变量保留引用
- (IBAction)startButtonPressed:(id)sender {
timer1 = [[NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(timerFunction:) userInfo:nil repeats: YES] retain];
}
另外,请确保释放计时器1,并在使用完毕后将其设置为零。和
[timer1 release]
释放了自动释放的对象,因此除非您保留了它,否则不要这样做释放了自动释放的对象,所以不要这样做,除非您保留了它。