【代码笔记】iOS-performSelectorOnMainThread

代码:

RootViewController.h

复制代码
#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController
{
    UILabel *label;
}

@end
复制代码

 

RootViewController.m

复制代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
 self.title=@"performSelectorOnMainThread"; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // performSelectorOnMainThread的作用就如其名,是在主线程执行某个selector, 所有的UIKit里的调用必须是在主线程的,在非主线程调用会产生非预期的结果也很可能会造成crash,所以当你在其他线程想执行一段UI调用的代码时,就需要用到这个方法了  [self performSelectorOnMainThread:@selector(loadData) withObject:nil waitUntilDone:NO]; } -(void)loadData { if (!label) { label=[[UILabel alloc]init]; label.frame=CGRectMake(50, 80, 200, 200); label.backgroundColor=[UIColor redColor]; [self.view addSubview:label]; }else { [label removeFromSuperview]; label=nil; } }
复制代码

转载于:https://www.cnblogs.com/yang-guang-girl/p/6970356.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值