gpt4 book ai didi

iphone - NStimer——我在这里做错了什么?

转载 作者:行者123 更新时间:2023-12-03 16:08:14 25 4
gpt4 key购买 nike

我已经成功使用 NSTimer,但现在遇到了问题。无疑是愚蠢的事情。欣赏另一双眼睛。运行调试器,我看到 applicationDidFinishLaunching 被调用,但触发器从未被调用。

-(void) trigger:(NSTimer *) theTimer{
NSLog(@"timer fired");
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {

nst = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(trigger) userInfo:nil repeats:YES];

[window makeKeyAndVisible];
}

最佳答案

选择器必须具有以下签名:

- (void)timerFireMethod:(NSTimer*)theTimer

所以你需要

@selector(trigger:)

--编辑--

也许您在其他地方执行此操作,但在您包含的代码中,您实际上并未启动计时器。您必须将其添加到 NSRunLoop 中,然后它才能触发任何事件。

 [[NSRunLoop currentRunLoop] addTimer:nst forMode:NSDefaultRunLoopMode];

如果我正确地阅读了示例。我只使用了一个 init 方法,该方法会自动将其添加到当前的 NSRunLoop 中。您确实应该查看有人在我的帖子评论中包含的开发人员文档。

关于iphone - NStimer——我在这里做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2218515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com