您的位置:首页 > 产品设计 > UI/UE

子线程中无法刷新更新 UI

2014-11-26 13:41 148 查看
显示结果:



//
//  RootViewController.m
//  多线程-
//
//  Created by zm on 14-11-26.
//  Copyright (c) 2014年 practice. All rights reserved.
//

#import "RootViewController.h"

@interface RootViewController ()<NSURLConnectionDataDelegate>
{
UIProgressView *_progressView;
}
@end

@implementation RootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {

}
return self;
}

/**
*  测试子线程更新 UI
*  唯一的情况只能是,子线程执行完毕,主线程中的值发生改变,主线程最终刷新改变 UI
*  现在的iOS版本,子线程中刷新界面的操作有时可能成功,已经部分支持了。但官方文档规定是主线程负责 UI 刷新界面
*/
-(void)testNSTreadFreshUI
{
_progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 100, 320, 44)];
[self.view addSubview:_progressView];

[self performSelectorInBackground:@selector(testNSTreadProgress) withObject:nil];

}

-(void)testNSTreadProgress
{
int progress = 0;
for (int i = 0; i < 1000; i++) {
progress += i;
[NSThread sleepForTimeInterval:0.1];

_progressView.progress = progress/1000.f;
}
}

- (void)viewDidLoad
{
[super viewDidLoad];

[self testNSTreadFreshUI];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: