第二种:NSObject

时间:2023-03-09 17:13:34
第二种:NSObject

第二种:NSObject

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