iOS NSThreadGCD 线程与队列(一)

//
//  NSThreadVController.m
//  ZM_NSThreadGCD
//
//  Created by ZM on 2015/2/9.
//  Copyright © 2015 ZM. All rights reserved.
//

#import "NSThreadVController.h"

@interface NSThreadVController ()
{
   
UILabel *my_label;
}
@end

@implementation NSThreadVController

- (
void )viewDidLoad
{
    [
super viewDidLoad ];
   
self . title = @"NSThreadVC" ;

   
my_label = [[ UILabel alloc ] initWithFrame : CGRectMake ( 80 , 80 , 150 , 60 )];
   
my_label . backgroundColor =[ UIColor cyanColor ];
   
my_label . text = @" 幸福每一天 " ;
   
my_label . font =[ UIFont systemFontOfSize : 15 ];
   
my_label . textAlignment = NSTextAlignmentCenter ;
    [
self . view addSubview : my_label ];
   
    [
self addBtnTitle : @" 开辟分线程 " frame : CGRectMake ( 10 , 200 , 200 , 35 ) Tag : 111 ];

}


- (
void )myBtnClick:( UIButton *)Btn{
   
   
NSDictionary *params = @{ @"key1" : @"value1" ,
                            
@"key2" : @"value2" } ;
   
// 开辟分线程
    [
NSThread detachNewThreadSelector : @selector (newThread:) toTarget : self withObject :params];
}


// 分线程
-(
void )newThread:( NSMutableDictionary *)dic
{
   
// 创建线程
   
// 判断 BOOL
   
NSThread *thread = [ NSThread currentThread ];
   
BOOL isMainThread = [thread isMainThread ];
   
   
NSLog ( @"---> thread = %d" ,isMainThread);
   
NSLog ( @"---> dic = %@ \n " ,dic);

   
// 创建 主线程
   
// 去网络请求获取数据 请求完成 必须要回到主线程刷新数据
    [
self performSelectorOnMainThread : @selector (mainThread:) withObject : @" 在主线程刷新数据 " waitUntilDone : YES ];
   
   
//    //1. 直接在开辟的线程中可以刷新数据
//    my_label.text = [dic objectForKey:@"key1"];
//
//    //2. 无论在分线程或者主线程中 再开辟一个分线程:都不能刷新数据
//    [NSThread detachNewThreadSelector:@selector(againNewThread_Two:) toTarget:self withObject:@{@"labelText": @"label"} ];
   
   
}
// 主线程
-(
void )mainThread:( NSString *)str
{
   
//1. 主线程刷新数据
   
NSLog ( @"---> str: %@" ,str);
   
my_label . text = str;
   
   
   
//2. 无论在分线程或者主线程中 再开辟一个分线程:都不能刷新数据
//    [NSThread detachNewThreadSelector:@selector(againNewThread_Two:) toTarget:self withObject:@{@"labelText": @"label"} ];
   
}

// 在线程中:再开辟一个分线程
-(
void )againNewThread_Two:( NSMutableDictionary *)dic
{
   
NSString *str= [dic objectForKey : @"label" ];
   
NSLog ( @"---> my_label.text: %@ \n " ,str);
   
my_label . text = str;
}


@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值