runloop(PreformSelector方法运用)

 1 @interface ViewController ()
 2 @end
 3 
 4 @implementation ViewController
 5 
 6 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
 7 {
 8     
10     [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
11     
12     
13     //同样开启的是子线程
14 //   NSThread *r = [[NSThread alloc]initWithTarget:self selector:@selector(run) object:nil];
15 //    [r start];
16     
17 }
18 
19 
20 - (void)run
21 {
22     
23     NSLog(@"run----%@",[NSThread currentThread]);
24     
25     
26     //为什么没执行task :注意这个方法虽然内部会自动加入runloop中,但这条子线程中的runloop还没开启,子线程中的runloop是需要手动开启的
27  //该方法内部自动将performSelector 事件添加到当前的runloop中,指定的运行模式为默认
28     [self performSelector:@selector(task) withObject:nil afterDelay:1.0];
29     
30     //开启runloop
31     [[NSRunLoop currentRunLoop]run];
32 }
33 
34 
35 - (void)task
36 {
37     
38     NSLog(@"task----%@",[NSThread currentThread]); //子线程
39 }

 

posted @ 2016-08-24 21:12  东爵  阅读(614)  评论(0)    收藏  举报