首页 » IOS » iOS 三种从子线程刷新主线程的方法

iOS 三种从子线程刷新主线程的方法

原文 http://blog.csdn.net/z979451341/article/details/79096275

2018-01-19 02:01:20阅读(197)

1.这个performSelector函数我们经常使用,他是用来执行一个函数用的,但是执行函数的线程是根据这个代码所在线程决定的,但是这可以通过performSelectorOnMainThread来决定执行函数在主线程执行,然后performSelectorInBackground能能够让执行函数在子线程。

-(void)fourAction{
    
    [self performSelectorInBackground:@selector(mid) withObject:nil];
    
    
    
}

-(void)mid{
    self.now++;
    
    [self performSelectorOnMainThread:@selector(change:) withObject:[NSString stringWithFormat:@"%d",self.now] waitUntilDone:NO];
}

-(void)change:(NSString *)text{
    self.label.text = text;
}

2.NSOperation也能够刷新主线程

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        self.now++;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            // UI更新代码
            self.label.text = [NSString stringWithFormat:@"%d",self.now];
        }];
    });


3.最后自然还有GCD,给人觉得最后一种最好用

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        self.now++;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.label.text = [NSString stringWithFormat:@"%d",self.now];
        });
    });


完整例子代码
//
//  ViewController.m
//  First
//
//  Created by shanreal-iOS on 17/10/16.
//  Copyright © 2017年 shanreal.LongZhenHao. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UIButton *btn1;
@property(nonatomic,strong)UIButton *btn2;
@property(nonatomic,strong)UIButton *btn3;
@property(nonatomic,strong)UIButton *btn4;
@property(nonatomic,strong)UILabel *label;
@property(nonatomic,assign)int now;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.now = 1;
    self.btn1 = [[UIButton alloc]initWithFrame:CGRectMake(10, 30, 100, 30)];
    [self.btn1 setTitle:@"one" forState:UIControlStateNormal];
    [self.btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn1 addTarget:self action:@selector(oneAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn1];
    
    self.btn2 = [[UIButton alloc]initWithFrame:CGRectMake(10, 70, 100, 30)];
    [self.btn2 setTitle:@"two" forState:UIControlStateNormal];
    [self.btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn2 addTarget:self action:@selector(twoAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn2];
    
    self.btn3 = [[UIButton alloc]initWithFrame:CGRectMake(10, 110, 100, 30)];
    [self.btn3 setTitle:@"three" forState:UIControlStateNormal];
    [self.btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn3 addTarget:self action:@selector(threeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn3];
    
    self.btn4 = [[UIButton alloc]initWithFrame:CGRectMake(10, 150, 100, 30)];
    [self.btn4 setTitle:@"four" forState:UIControlStateNormal];
    [self.btn4 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.btn4 addTarget:self action:@selector(fourAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn4];
    
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(10, 190, 200, 30)];
    [self.view addSubview:self.label];
    
}
-(void)oneAction{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        self.now++;
        
        [self performSelectorOnMainThread:@selector(change:) withObject:[NSString stringWithFormat:@"%d",self.now] waitUntilDone:NO];
        
    });
    
    
}
-(void)twoAction{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        self.now++;
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            // UI更新代码
            self.label.text = [NSString stringWithFormat:@"%d",self.now];
        }];
    });
    
    
    
}
-(void)threeAction{
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        self.now++;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            self.label.text = [NSString stringWithFormat:@"%d",self.now];
        });
    });
    
    
    
}
-(void)fourAction{
    
    [self performSelectorInBackground:@selector(mid) withObject:nil];
    
    
    
}
-(void)mid{
    self.now++;
    
    [self performSelectorOnMainThread:@selector(change:) withObject:[NSString stringWithFormat:@"%d",self.now] waitUntilDone:NO];
}
-(void)change:(NSString *)text{
    self.label.text = text;
}
@end


最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹