美文网首页
Runloop开启常驻线程

Runloop开启常驻线程

作者: Lee_Jo | 来源:发表于2020-05-18 18:00 被阅读0次
    #import "ViewController.h"
    
    @interface ViewController ()
    @property (nonatomic, strong) NSThread *thread;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10, 400, 330, 34);
        [button addTarget:self action:@selector(click:)
         forControlEvents:UIControlEventTouchUpInside];
        button.backgroundColor = [UIColor grayColor];
        [self.view addSubview:button];
        self.thread = [[NSThread alloc] initWithTarget:self
                                              selector:@selector(excute:)
                                                object:nil];
        [self.thread start];
    }
    
    - (void)excute:(id)sender {
        @autoreleasepool {
            NSRunLoop *loop = [NSRunLoop currentRunLoop];
            [loop addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
            [loop run];
        }
    }
    
    - (void)click:(id)sender {
        [self performSelector:@selector(excute:)
                     onThread:self.thread
                   withObject:nil
                waitUntilDone:NO];
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:Runloop开启常驻线程

          本文链接:https://www.haomeiwen.com/subject/xudpohtx.html