如果计时器过期,我想调用一个方法,但是不会调用该方法。不确定出了什么问题。有什么建议吗?
被召唤:
- (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]];
}
}
答案 0 :(得分:1)
我通过在当前运行循环中添加计时器并运行来解决它。感谢。
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];
}