iphone UIProgressView 进度使用例子
待实现功能:打开首页面时,应用从服务器下载数据,首页上的进度条显示数据下载的进度:
步骤一:在xib 中 添加 UIProgressView,UILabel两个控件;
步骤二:编码;
接口:
@interface BetaTownViewController : UIViewController
{
IBOutlet UIProgressView *progressView;//进度条控件
IBOutlet UILabel *lblMessage;//用于进度显示
float progress;//存放进度值
NSString * progressMsg;//存放进度信息
}
@property (nonatomic,retain) UIProgressView *progressView;
@property (nonatomic,retain) NSString *progressMsg;
@property (nonatomic) float progress;
实现:
#import "BetaTownViewController.h"
@implementation BetaTownViewController
@synthesize progressView;
@synthesize progress;
@synthesize progressMsg;
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad{
[super viewDidLoad];
}
- (void)viewDidUnload{
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
}
-(void) updateProgress{
[progressView setProgress:progress];
[lblMessage setText:progressMsg];
}
-(void) initDATA{
progress =0.1;
progressMsg = @"开始调用方法1,请稍后...";
[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];
//调用方法1
progress =0.3;
progressMsg = @"开始调用方法2,请稍后...";
[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];
//调用方法2
progress =0.7;
progressMsg = @"开始调用方法3,请稍后...";
[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];
//调用方法3
progress =1.0;
progressMsg = @"方法调用完毕";
[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];
}
- (void)viewDidAppear:(BOOL)animated{
//开启线程,进行下载数据
[NSThread detachNewThreadSelector:@selector(initDATA) toTarget:self withObject:nil];
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
我把部分代码删除掉了,但思路是这样的,要启用一下线程来更新进度值;
最后还是希望大家在写技术贴时,不要copy来,copy去,否则浪费其他人的时间。