码迷,mamicode.com
首页 > 其他好文 > 详细

NSThread

时间:2015-03-31 17:35:01      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:

//
//  ViewController.m
//  thread
//
//  Created by City--Online on 15/3/31.
//  Copyright (c) 2015年 City--Online. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    NSThread *thread1;
    NSThread *thread2;
    UIButton *btn;

}
 @end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     btn=[UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(30, 30, 50, 50);
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    btn.backgroundColor=[UIColor greenColor];
    [self.view addSubview:btn];
    //线程创建 主要有两种方法
     thread1=[[NSThread alloc] initWithTarget:self selector:@selector(threadfun1) object:nil];
     [thread1 start];//此方法创建需要手动启动
    //此方法自动启动线程方法
     [NSThread detachNewThreadSelector:@selector(threadfun2) toTarget:self withObject:nil];

}
-(void)threadfun1
{
    NSLog(@"thread1");
    //调用主线程更新
    [self performSelectorOnMainThread:@selector(upbutton) withObject:nil waitUntilDone:NO];
   
}
-(void)upbutton
{
    [btn setTitle:@"123" forState:UIControlStateNormal];
    [self performSelector:@selector(threadtothread) withObject:nil];
    
}
-(void)threadtothread
{
    NSLog(@"threadtothread");
}
-(void)threadfun2
{
    NSLog(@"thread2");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

 

NSThread

标签:

原文地址:http://www.cnblogs.com/cuiyw/p/4381404.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!