Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-计时器调用问题_Ios_Objective C_Nstimer_Nsinvocation - Fatal编程技术网

iOS-计时器调用问题

iOS-计时器调用问题,ios,objective-c,nstimer,nsinvocation,Ios,Objective C,Nstimer,Nsinvocation,我想在计时器过期时调用一个方法,但该方法没有被调用。不知道出了什么问题。有什么建议吗 被调用: - (void)messageSendingReply:(id)messageID { //Do something. } - (void)sendingMessageTimer_Start:(int64_t)sendingMsgID Time:(NSTimeInterval)replyDelay { NSMethodSignature *sig = [self methodSig

我想在计时器过期时调用一个方法,但该方法没有被调用。不知道出了什么问题。有什么建议吗

被调用:

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

    }

}
调用上述命令:

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

是否将计时器添加到运行循环中?