我想在计时器到期时调用一个方法,但该方法没有被调用。不知道出了什么问题。有什么建议吗?
被调用:
- (void)messageSendingReply:(id)messageID
{
//Do something.
}
调用上面的:
- (void)sendingMessageTimer_Start:(int64_t)sendingMsgID Time:(NSTimeInterval)replyDelay {
NSMethodSignature *sig = [self methodSignatureForSelector:@selector(messageSendingReply:)];
if (sig)
{
NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];
[invo setTarget:self];
[invo setSelector:@selector(messageSendingReply:)];
id obj= [NSNumber numberWithLongLong:sendingMsgID];
[invo setArgument:&obj atIndex:0];
[invo retainArguments];
[self.replyTimerDic setValue:[NSTimer scheduledTimerWithTimeInterval:replyDelay invocation:invo repeats:NO] forKey:[NSString stringWithFormat:@"%qi",sendingMsgID]];
}
}
最佳答案
我通过在当前运行循环上添加计时器并运行来解决它。谢谢。
if (sig)
{
NSInvocation* invo = [NSInvocation invocationWithMethodSignature:sig];
[invo setTarget:self];
[invo setSelector:@selector(messageSendingReply:)];
id obj= [NSNumber numberWithLongLong:sendingMsgID];
[invo setArgument:&obj atIndex:0];
[invo retainArguments];
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:replyDelay invocation:invo repeats:NO];
[self.replyTimerDic setValue:timer forKey:[NSString stringWithFormat:@"%qi",sendingMsgID]];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSRunLoopCommonModes];
[runLoop run];
}
关于iOS - 计时器调用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25926147/