performSelector

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay;

--1--

知识点:这个方法是单线程的,也就是说只有当前调用此方法的函数执行完毕后,selector方法才会被调用。

复制代码
- (void)mainMethod
{   
    [self performSelector:@selector(delayMethod) withObject:nil afterDelay:1];

    NSLog(@"调用方法==开始");
    
    sleep(5);
    
    NSLog(@"调用方法==结束");
}

- (void)delayMethod
{
    NSLog(@"执行延迟方法");
}

执行结果(注意log打印的顺序):

  调用方法==开始
  调用方法==结束
  执行延迟方法
复制代码

实战经验:正因为本方法是一个单线程的,所以有些时候,我们会遇到虽然调用了这个方法,但是不执行的问题。这时,我们需要将原代码改造如下:

改造前代码:无法执行延迟方法

复制代码
-(void)someMethod
{
      
    [self performSelector:@selector(labelDidDisappeared:) withObject:label afterDelay:2];   //因为某些原因,不被调用,没有执行

}

-(void)labelDidDisappeared:(UILabel *)label

{
    [label removeFromSuperview];

    [label release];
}
复制代码

改造后代码:  可以正常执行延迟方法

复制代码
-(void)someMethod
{
    //关键
    [self performSelectorOnMainThread:@selector(labelWillDisappeared:) withObject:label waitUntilDone:NO];  

}
  
-(void)labelWillDisappeared:(UILabel *)label
{
    [self performSelector:@selector(labelDidDisappeared:) withObject:label afterDelay:2];
}

-(void)labelDidDisappeared:(UILabel *)label
{
    [label removeFromSuperview];
    [label release];
}
复制代码

 知识扩展:以下两个方法均是多线程方法

- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;

参考:http://blog.csdn.net/baxiaxx/article/details/7878338  感谢原作者的无私和奉献!

 

--2--

知识点:该方法只能接受一个参数。如果需要传递多个参数怎么办呢? 让selector调用的方法接受的参数类型修改为Dictionary类型。

注意: 

1)如果调用的selector不接受参数则,withObjectnil 

2 通过performSelector:withObjcet:afterDelay调用的方法不能有返回值

 知识扩展:performSelector 带多个参数

复制代码
//第一个试验:带一个参数
- (void) fooOneIput:(NSString*) first {
NSLog(
@"Logs %@", first);
}
//然后调用它
[self performSelector:@selector(fooOneInput:) withObject:
@"first"];
//第二个试验:带多个参数
- (void) fooFirstInput:(NSString*) first secondInput:(NSString*) second {
NSLog(
@"Logs %@ then %@", first, second);
}
//然后调用它
[self performSelector:@selector(fooFirstInput:secondInput:) withObject:
@"first" withObject:@"second"];
复制代码

实战经验:经实验,只有performSelector: withObject: 方法可以带多个参数,其它类似的perform方法均不行,包括和此方法最类似的performSelectorInBackground:withObject: 方法。

 

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;


代码如下:


- (void)changeText:(NSString *)string

{

    label.text = string;

    NSLog(@"changeText:(NSString *)string");

}


- (void)changePopoverSize

{

    [self performSelectorOnMainThread:@selector(changeText:) withObject:@"Happy aha111" waitUntilDone:YES];

    NSLog(@"changePopoverSize#####end");

    sleep(5);

    NSLog(@"changePopoverSize-----end");

}

执行结果如下:


2012-08-17 17:19:29.618 awrbv[2024:f803] changeText:(NSString *)string

2012-08-17 17:19:29.619 awrbv[2024:f803] changePopoverSize#####end

2012-08-17 17:19:34.620 awrbv[2024:f803] changePopoverSize-----end

可以看出,如果waitUntilDone:YES那么等changeText执行完毕后再往下执行

如果waitUntilDone:NO的话,结果如下:


2012-08-17 17:21:12.135 awrbv[2049:f803] changePopoverSize#####end

2012-08-17 17:21:17.137 awrbv[2049:f803] changePopoverSize-----end

2012-08-17 17:21:17.139 awrbv[2049:f803] changeText:(NSString *)string


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
- (void)close { // Empty queues. [self emptyQueues]; [partialReadBuffer release]; partialReadBuffer = nil; [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(disconnect) object:nil]; // Close streams. if (theReadStream != NULL) { CFReadStreamSetClient(theReadStream, kCFStreamEventNone, NULL, NULL); CFReadStreamUnscheduleFromRunLoop (theReadStream, theRunLoop, kCFRunLoopDefaultMode); CFReadStreamClose (theReadStream); CFRelease (theReadStream); theReadStream = NULL; } if (theWriteStream != NULL) { CFWriteStreamSetClient(theWriteStream, kCFStreamEventNone, NULL, NULL); CFWriteStreamUnscheduleFromRunLoop (theWriteStream, theRunLoop, kCFRunLoopDefaultMode); CFWriteStreamClose (theWriteStream); CFRelease (theWriteStream); theWriteStream = NULL; } // Close sockets. if (theSocket != NULL) { CFSocketInvalidate (theSocket); CFRelease (theSocket); theSocket = NULL; } if (theSocket6 != NULL) { CFSocketInvalidate (theSocket6); CFRelease (theSocket6); theSocket6 = NULL; } if (theSource != NULL) { CFRunLoopRemoveSource (theRunLoop, theSource, kCFRunLoopDefaultMode); CFRelease (theSource); theSource = NULL; } if (theSource6 != NULL) { CFRunLoopRemoveSource (theRunLoop, theSource6, kCFRunLoopDefaultMode); CFRelease (theSource6); theSource6 = NULL; } theRunLoop = NULL; // If the client has passed the connect/accept method, then the connection has at least begun. // Notify delegate that it is now ending. if (theFlags & kDidPassConnectMethod) { // Delay notification to give him freedom to release without returning here and core-dumping. if ([theDelegate respondsToSelector: @selector(onSocketDidDisconnect:)]) { //[theDelegate performSelector:@selector(onSocketDidDisconnect:) withObject:self afterDelay:0]; [theDelegate onSocketDidDisconnect:self]; } } // Clear flags. theFlags = 0x00; }
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值