#import "ViewController.h" #import @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButto... ...
#import "ViewController.h" #import <pthread.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(100, 100, 100, 30); [btn setTitle:@"pThread" forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [btn addTarget:self action:@selector(clickPThread) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)clickPThread { NSLog(@"我在主線程中執行!!!"); pthread_t pthread; pthread_create(&pthread, NULL, run, NULL); } // C語言寫法 void *run(void *data) { NSLog(@"我在子線程中執行!!!"); for (int i = 1; i < 10; i++) { NSLog(@"%d", i); sleep(1); } return NULL; }