标签:form work code load sam void 技术 uilabel cal
类型是id。
(我们能够传入不论什么參数)
waitUntilDone:YES指定,当前线程是否要被堵塞。直到主线程将我们制定的代码块运行完。
注意:
1.当前线程为主线程的时候,waitUntilDone:YES參数无效。
2.该方法。没有返回值
3.该方法主要用来用主线程来改动页面UI的状态。
sample code:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _label=[[UILabel alloc] initWithFrame:CGRectMake(40, 40, 60, 40)]; _label.textColor=[UIColor redColor]; _label.text=@"123"; [self.view addSubview:_label]; [self performSelectorInBackground:@selector(backWork) withObject:nil]; } -(void)backWork { NSLog(@"the thread is %@",[NSThread currentThread]); sleep(2); [self performSelectorOnMainThread:@selector(mainWork) withObject:nil waitUntilDone:NO]; } -(void)mainWork { NSLog(@"the main thread is %@",[NSThread currentThread]); _label.text=@"456"; _label.textColor=[UIColor greenColor]; }运行结果:
2014-08-19 11:03:59.101 testApp[1848:3107] the thread is <NSThread: 0x8c504a0>{name = (null), num = 2}
2014-08-19 11:04:01.103 testApp[1848:60b] the main thread is <NSThread: 0x8c444c0>{name = (null), num = 1}
利用performSelectorInBackground和performSelectorOnMainThread实现多线程
标签:form work code load sam void 技术 uilabel cal
原文地址:http://www.cnblogs.com/llguanli/p/7269953.html