欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS 关于取消延迟执行函数的种种。performSelector与cancelPreviousPerformRequestsWithTarget

程序员文章站 2023-01-24 20:26:32
print?
@interface nsobject (nsdelayedperformin...

print?<pre class=plain name="code">@interface nsobject (nsdelayedperforming) 
 
- (void)performselector:(sel)aselector withobject:(id)anargument afterdelay:(nstimeinterval)delay inmodes:(nsarray *)modes; 
- (void)performselector:(sel)aselector withobject:(id)anargument afterdelay:(nstimeinterval)delay; 
+ (void)cancelpreviousperformrequestswithtarget:(id)atarget selector:(sel)aselector object:(id)anargument; 
+ (void)cancelpreviousperformrequestswithtarget:(id)atarget; 
 
@end</pre><br> 
<br> 
<pre></pre> 
<pre class=cpp name="code"></pre><pre class=cpp name="code" sizcache="1" sizset="5">最近看到大家对cancelpreviousperformrequestswithtarget不能取消带参数的<pre class=cpp name="code">- (void)performselector:(sel)aselector withobject:(id)anargument afterdelay:(nstimeinterval)delay;</pre>情况,写了一点点代码调试了一下,发现如果是带参数,那取消时的参数也要一致,否则不能取消成功。 
<pre></pre> 
<pre class=cpp name="code"></pre><pre class=cpp name="code">开始延迟执行:</pre><pre class=cpp name="code">[self performselector:@selector(didrunincurrmodel:) withobject:[nsnumber numberwithbool:yes] afterdelay:3.0f]; 
     
[self performselector:@selector(didrunincurrmodelnoargument) withobject:nil afterdelay:3.0f];</pre> 
<p><br> 
</p> 
<p>假如在三秒内执行以下取消方法,则结果分别为:</p> 
<p></p> 
<pre class=cpp name="code">    [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:[nsnumber numberwithbool:yes]];//可以取消成功。  
     
    [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:[nsnumber numberwithbool:no]];//不能取消成功。参数不匹配  
     
    [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:nil];//不能取消成功。参数不匹配  
     
    [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodelnoargument) object:nil];//可以成功取消  
     
    [nsobject cancelpreviousperformrequestswithtarget:self];//可以成功取消全部。  
    [[self class] cancelpreviousperformrequestswithtarget:self];//可以成功取消全部。</pre>  
<p></p> 
<p><br> 
</p> 
<p><br> 
</p> 
<br> 
<br> 
<p><br> 
</p> 
<pre></pre> 
<pre></pre> 
 
</pre> 

[plain]
@interface nsobject (nsdelayedperforming)    - (void)performselector:(sel)aselector withobject:(id)anargument afterdelay:(nstimeinterval)delay inmodes:(nsarray *)modes;  - (void)performselector:(sel)aselector withobject:(id)anargument afterdelay:(nstimeinterval)delay;  + (void)cancelpreviousperformrequestswithtarget:(id)atarget selector:(sel)aselector object:(id)anargument;  + (void)cancelpreviousperformrequestswithtarget:(id)atarget;    @end  @interface nsobject (nsdelayedperforming)

- (void)performselector:(sel)aselector withobject:(id)anargument afterdelay:(nstimeinterval)delay inmodes:(nsarray *)modes;
- (void)performselector:(sel)aselector withobject:(id)anargument afterdelay:(nstimeinterval)delay;
+ (void)cancelpreviousperformrequestswithtarget:(id)atarget selector:(sel)aselector object:(id)anargument;
+ (void)cancelpreviousperformrequestswithtarget:(id)atarget;

@end


[cpp]
  [cpp]
最近看到大家对cancelpreviousperformrequestswithtarget不能取消带参数的<pre class=cpp name="code">- (void)performselector:(sel)aselector withobject:(id)anargument afterdelay:(nstimeinterval)delay;</pre>情况,写了一点点代码调试了一下,发现如果是带参数,那取消时的参数也要一致,否则不能取消成功。  <pre></pre>  <pre class=cpp name="code"></pre><pre class=cpp name="code">开始延迟执行:</pre><pre class=cpp name="code">[self performselector:@selector(didrunincurrmodel:) withobject:[nsnumber numberwithbool:yes] afterdelay:3.0f];        [self performselector:@selector(didrunincurrmodelnoargument) withobject:nil afterdelay:3.0f];</pre>  <p><br>  </p>  <p>假如在三秒内执行以下取消方法,则结果分别为:</p>  <p></p>  <pre class=cpp name="code">    [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:[nsnumber numberwithbool:yes]];//可以取消成功。             [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:[nsnumber numberwithbool:no]];//不能取消成功。参数不匹配             [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:nil];//不能取消成功。参数不匹配             [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodelnoargument) object:nil];//可以成功取消             [nsobject cancelpreviousperformrequestswithtarget:self];//可以成功取消全部。       [[self class] cancelpreviousperformrequestswithtarget:self];//可以成功取消全部。</pre>   <p></p>  <p><br>  </p>  <p><br>  </p>  <br>  <br>  <p><br>  </p>  <pre></pre>  <pre></pre>  最近看到大家对cancelpreviousperformrequestswithtarget不能取消带参数的[cpp] view plaincopyprint?- (void)performselector:(sel)aselector withobject:(id)anargument afterdelay:(nstimeinterval)delay;  - (void)performselector:(sel)aselector withobject:(id)anargument afterdelay:(nstimeinterval)delay;情况,写了一点点代码调试了一下,发现如果是带参数,那取消时的参数也要一致,否则不能取消成功。


[cpp] 
 [cpp]
开始延迟执行:  开始延迟执行:
[cpp]
[self performselector:@selector(didrunincurrmodel:) withobject:[nsnumber numberwithbool:yes] afterdelay:3.0f];        [self performselector:@selector(didrunincurrmodelnoargument) withobject:nil afterdelay:3.0f];  [self performselector:@selector(didrunincurrmodel:) withobject:[nsnumber numberwithbool:yes] afterdelay:3.0f];
   
[self performselector:@selector(didrunincurrmodelnoargument) withobject:nil afterdelay:3.0f];


假如在三秒内执行以下取消方法,则结果分别为:

[cpp]  [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:[nsnumber numberwithbool:yes]];//可以取消成功。     [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:[nsnumber numberwithbool:no]];//不能取消成功。参数不匹配     [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:nil];//不能取消成功。参数不匹配     [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodelnoargument) object:nil];//可以成功取消     [nsobject cancelpreviousperformrequestswithtarget:self];//可以成功取消全部。   [[self class] cancelpreviousperformrequestswithtarget:self];//可以成功取消全部。      [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:[nsnumber numberwithbool:yes]];//可以取消成功。
   
    [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:[nsnumber numberwithbool:no]];//不能取消成功。参数不匹配
   
    [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodel:) object:nil];//不能取消成功。参数不匹配
   
    [nsobject cancelpreviousperformrequestswithtarget:self selector:@selector(didrunincurrmodelnoargument) object:nil];//可以成功取消
   
    [nsobject cancelpreviousperformrequestswithtarget:self];//可以成功取消全部。
    [[self class] cancelpreviousperformrequestswithtarget:self];//可以成功取消全部。