2014-09-19 32 views
0

如果定時器過期,我想調用一個方法,但該方法不會被調用。不知道發生了什麼問題。有什麼建議麼?iOS - 定時器調用問題

被稱爲:

- (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]]; 

    } 

} 
+1

您是否將定時器添加到運行循環? – 2014-09-19 04:57:43

回答

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]; 
}