IOS--GCD基础
TestController.m
1 #import "TestController.h" 2 #import "TestView.h" 3 4 @interface TestController() 5 6 @property(nonatomic,strong)UIButton *button; 7 8 @end 9 10 @implementation TestController 11 12 - (void)viewDidLoad 13 { 14 [super viewDidLoad]; 15 16 _button = [UIButton buttonWithType:UIButtonTypeSystem]; 17 18 _button.frame = CGRectMake(0, 20, 100, 20); 19 [_button setTitle:@"Hello" forState:UIControlStateNormal]; 20 21 [_button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside]; 22 23 [self.view addSubview:_button]; 24 25 } 26 27 //GCD 28 -(void)start:(UIButton*)sender 29 { 30 dispatch_queue_t queeue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 31 dispatch_async(queeue, ^{ 32 [self thLoop]; 33 }); 34 } 35 36 -(void)thLoop 37 { 38 for (int i =1; i<=10 ; i++) { 39 40 //睡眠1秒 41 [NSThread sleepForTimeInterval:1]; 42 NSLog(@"i=%d",i); 43 } 44 45 NSLog(@"end"); 46 47 } 48 49 50 @end