iOS - 计时器调用问题

标签 ios objective-c nstimer nsinvocation

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

被调用:

- (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/

相关文章:

iphone - 在 UITableViewCell 中访问 UITextField

swift - 按下后在 uiviewcontroller 内部不调用 deinit - Swift

iphone - 应用程序在屏幕关闭时不播放声音,但仍按应有的方式执行其他操作?

objective-c - 图像出现像素化

ios - 自定义 NSLog 方法(可变参数)

ios - UIScrollView 在滚动时暂停 NSTimer

ios - AutoLayout 内容不适合 UITableView 单元格

ios - Swift 如何在 uicollectionviewcell 的子元素上添加点击手势

ios - 在应用程序购买中,以编程方式禁用自动更新

ios - unregisterForRemoteNotifications 不适用于 iOS8 - 推送通知