IOS--多线程基础
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 - (void)viewDidLoad 11 { 12 [super viewDidLoad]; 13 14 _button = [UIButton buttonWithType:UIButtonTypeSystem]; 15 16 _button.frame = CGRectMake(0, 20, 100, 20); 17 [_button setTitle:@"Hello" forState:UIControlStateNormal]; 18 19 [_button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside]; 20 21 [self.view addSubview:_button]; 22 23 } 24 25 26 -(void)start:(UIButton*)sender 27 { 28 NSThread *t = [[NSThread alloc]initWithTarget:self selector:@selector(thLoop) object:nil]; 29 30 //启动线程 31 [t start]; 32 } 33 34 -(void)thLoop 35 { 36 for (int i =1; i<=10 ; i++) { 37 38 //睡眠1秒 39 [NSThread sleepForTimeInterval:1]; 40 NSLog(@"i=%d",i); 41 } 42 43 NSLog(@"end"); 44 45 } 46 47 @end