• IOS开发:关于performSelectorXXX的延迟的使用


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

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

    比如:

    - (void)changeText:(NSString *)string

    {

        label.text = string;

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

    }

    - (void)changePopoverSize

    {   

        [self performSelector:@selector(changeText:) withObject:@"Happy aha" afterDelay:1];

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

        sleep(5);

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

    }

    执行结果(注意时间):

    2012-08-17 17:14:06.697 awrbv[1973:f803] changePopoverSize#####end

    2012-08-17 17:14:11.698 awrbv[1973:f803] changePopoverSize-----end

    2012-08-17 17:14:11.701 awrbv[1973:f803] changeText:(NSString *)string

    如果要想多线程的话,可以是使用

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

    或者

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

     

  • 相关阅读:
    c++字符串排序
    JAVA实现四则运算的简单计算器
    JAVA图形小动画之简单行星运动
    JAVA多线程编程
    ege图形库之简单贪吃蛇(c++)
    ege图形库之动画排序
    mysql 性能优化方案
    MYSQL 优化常用方法
    [手把手教你] 用Swoft 搭建微服务(TCP RPC)
    php有效防止同一用户多次登录
  • 原文地址:https://www.cnblogs.com/ChouDanDan/p/5784889.html
Copyright © 2020-2023  润新知