1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 /** 4 * 开启子线程的方式之一:NSObject 5 */ 6 // 第一个参数:selector 7 // 第二个参数:方法传递的参数 8 [self performSelectorInBackground:@selector(threadAction) withObject:@"test"]; 9 self.view.backgroundColor = [UIColor cyanColor]; 10 }
1 // 实现子线程的方法 2 - (void)threadAction { 3 // NSLog(@"hello world"); 4 // NSLog(@"current = %@", [NSThread currentThread]); 5 // NSLog(@"main = %@", [NSThread mainThread]); 6 // 回到主线程修改当前的背景颜色 7 // 第三个参数:是否等待子线程完成之后进入主线程 8 [self performSelectorOnMainThread:@selector(mainThreadction) withObject:nil waitUntilDone:YES]; 9 } 10 11 // 实现主线程的方法 12 - (void)mainThreadction { 13 self.view.backgroundColor = [UIColor orangeColor]; 14 NSLog(@"current = %@", [NSThread currentThread]); 15 NSLog(@"main = %@", [NSThread mainThread]); 16 }