执行选择器延迟后不起作用

发布于 2025-01-07 21:44:01 字数 519 浏览 2 评论 0原文

我在 uiview 子类中使用以下方法:

[self performSelector:@selector(timeout) withObject:nil afterDelay:20];

该方法按预期在 20 秒后调用。 在另一种方法中,我尝试使用以下代码取消执行请求:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timeout) object:nil];

我也尝试过

[NSRunLoop cancelPreviousPerformRequestsWithTarget:self selector:@selector(timeout) object:nil];

这两条消息都没有带来预期的结果,并且仍然调用超时方法。 谁能解释一下我做错了什么以及如何以正确的方式做到这一点?

来自奥地利的欢呼 马丁

i am using the following method in a uiview subclass:

[self performSelector:@selector(timeout) withObject:nil afterDelay:20];

The method is called after 20 seconds as expected.
In another method i try to cancel the perform request using the following code:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timeout) object:nil];

i've also tried

[NSRunLoop cancelPreviousPerformRequestsWithTarget:self selector:@selector(timeout) object:nil];

both messages don't bring the expected result an the timeout method is still called.
can anybody explain me what i am doing wrong and how to do it the right way ?

cheers from austria
martin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不即不离 2025-01-14 21:44:01

两点
1. 两个 self 是同一个对象吗?
2. [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timeout) object:nil]; 是否在您调用 [self PerformSelector:@selector(timeout) withObject:nil 的同一线程上执行afterDelay:20];

检查这两个问题。

Two points
1. Are both self same object??
2. Is [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timeout) object:nil]; performed on same thread on which you called [self performSelector:@selector(timeout) withObject:nil afterDelay:20]; ?

Check these two problems.

毁虫ゝ 2025-01-14 21:44:01

使用作为实例变量存储在类中的 NSTimer。当你想取消执行时,使定时器失效并销毁。

在您的@interface中:

@property (readwrite, retain) NSTimer *myTimer;

在您的@implementation中:

self.myTimer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(timeout) userInfo:nil repeats:NO];

然后,如果发生某种情况并且不应再调用超时方法:

[self.myTimer invalidate];
self.myTimer = nil; // this releases the retained property implicitly

Use an NSTimer stored as an instance variable in your class. When you want to cancel the perform, invalidate and destroy the timer.

In your @interface:

@property (readwrite, retain) NSTimer *myTimer;

In your @implementation:

self.myTimer = [NSTimer scheduledTimerWithTimeInterval:20 target:self selector:@selector(timeout) userInfo:nil repeats:NO];

Then, if some condition happens and the timeout method should no longer be called:

[self.myTimer invalidate];
self.myTimer = nil; // this releases the retained property implicitly
ぽ尐不点ル 2025-01-14 21:44:01

试试这个:

[self performSelectorOnMainThread:@selector(timeout) withObject:self waitUntilDone:NO];

Try this:

[self performSelectorOnMainThread:@selector(timeout) withObject:self waitUntilDone:NO];
樱花落人离去 2025-01-14 21:44:01

您可以通过两种方式做到这一点:

  1. 您可以使用此方法来删除所有排队的内容

    [NSObject cancelPreviousPerformRequestsWithTarget:self];

  2. 您可以单独删除每一个

    [NSObject cancelPreviousPerformRequestsWithTarget:self
    选择器:@selector(超时)
    object:nil];

You can do that with 2 ways :

  1. You could use this which would remove all queued

    [NSObject cancelPreviousPerformRequestsWithTarget:self];

  2. you can remove each one individually

    [NSObject cancelPreviousPerformRequestsWithTarget:self
    selector:@selector(timeout)
    object:nil];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文