可能重复:
NSTimer doesn't stop
我很难停止计时器,因为我的服务器已经停止了。 我已经在这里和其他地方搜索了其他答案,但我似乎无法找到我出错的地方。
我决定用相同的想法制作一个示例代码,但是,你单击一个按钮,计时器启动,你点击另一个计时器结束,它按照它应该的方式工作。请不要介意我做错了什么(除了计时器部分)我是新手。所有我想知道的是为什么它不会停止..
提前致谢。
Connection.h
#import <Foundation/Foundation.h>
@interface Connection : NSObject
{
NSString *urlString;
NSURL *url;
NSMutableURLRequest *request;
NSURLConnection *connection;
NSURLResponse *response;
NSMutableData *receivedData;
NSData *responseData;
NSError *error;
NSTimer *timer;
}
@property (nonatomic, retain) NSTimer *timer;
-(BOOL)authenticateUser:(NSString *)userName Password:(NSString *)password;
-(BOOL)checkConnection;
-(void)ping:(NSTimer *)aTimer;
-(void)logout;
-(void)timerStart;
-(void)timerStop;
@end
Connection.m
#import "Connection.h"
#import "Parser.h"
#import "Reachability.h"
#import "TBXML.h"
@implementation Connection
@synthesize timer;
-(BOOL) authenticateUser:(NSString *)userName Password:(NSString *)password
{
BOOL success;
urlString = [[NSString alloc] initWithFormat:@"my/server/address/login"];
url =[[NSURL alloc] initWithString:urlString];
request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
error = [[NSError alloc] init];
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[responseData retain];
NSString *tempString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSMutableDictionary *tempDict= [[NSMutableDictionary alloc] init];
if (request)
{
Parser *parser = [[Parser alloc] init];
tempDict = [parser readXMLString:tempString];
for (id key in tempDict)
{
NSLog(@"%@ is %@",key,[tempDict objectForKey:key]);
}
if ([[tempDict objectForKey:@"login"] isEqualToString:@"true"] )
{
success = YES;
self.timerStart;
}
else
{
success = NO;
}
}
[urlString release];
[url release];
[error release];
[responseData release];
[tempString release];
return success;
}
-(void)logout
{
self.timerStop;
}
-(void)ping:(NSTimer *)aTimer;
{
urlString = [[NSString alloc] initWithFormat:@"my/server/address"];
url =[[NSURL alloc] initWithString:urlString];
request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
NSLog(@"ping");
[urlString release];
[url release];
}
-(BOOL)checkConnection
{
Reachability *reachability = [Reachability reachabilityWithHostName:@"http://my/server/address"];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
return NO;
}
else
{
return YES;
}
}
-(void)timerStart
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ping:) userInfo:nil repeats:YES];
}
-(void)timerStop
{
[self.timer invalidate];
self.timer = nil;
}
@end
答案 0 :(得分:2)
在timerStart中,您只需替换timer属性中的任何内容即可。如果你开始第二个计时器而不停止第一个计时器,它将永远运行。所以timerStart应该在创建一个新的之前首先调用timerStop(并且应该有一个新名称,因为从timerStart调用timerStop是愚蠢的。)
答案 1 :(得分:0)
使用点句语法{{1}}仅适用于属性(如果不这样做,则会生成警告),而不是按照您的方式调用方法。
编辑:这不会解决您的问题,但按照您的方式进行操作非常错误的编码习惯
答案 2 :(得分:0)
-(void)timerStart
{
[self.timer invalidate];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(ping:) userInfo:nil repeats:YES];
}