为iOS应用程序开发等待控件
UIActivityIndicatorView
在iOS中开发一个等待控件其实也跟对话框一样特别简单
第一步:把所需要的控件拖入到屏幕当中,如下图
第二步:在ViewController.h文件中加入如下内容,在ViewController.m文件中加入实现方法
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
//定义一个等待控件
IBOutlet UIActivityIndicatorView * activityIndicatorView;
}
//提供GET SET
@property(nonatomic,retain)UIActivityIndicatorView * activityIndicatorView;
//按钮的点击事件
-(IBAction)prass:(id)sender;
@end
ViewController.m
@implementation ViewController
//实现GET SET
@synthesize activityIndicatorView;
-(IBAction)prass:(id)sender{
if ([activityIndicatorView isAnimating]) {
[activityIndicatorView stopAnimating];
}else{
[activityIndicatorView startAnimating];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//消除内存
- (void)dealloc
{
[activityIndicatorView release];
[super dealloc];
}
@end
第三步在NIB文件中进行关联 大慨就是按照箭头所指的方向进行关联
就可以实现如下显示的效果
注意:如何让控件在启动的时候不显示,停止的时候消失呢?只有在运行的时候才显示呢?答案很简单只要设置一个属性就行啦
Progress NSTimer
在iOS中开发一个进度条其实也跟等待一样特别简单
第一步:开发界面
第二步编码
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController{
IBOutlet UIProgressView *progressview;
NSTimer *timer;
}
@property(nonatomic,retain) UIProgressView *progressview;
@property(nonatomic,assign) NSTimer *timer;
-(IBAction)Onclick;
@end
ViewController.m
@implementation ViewController
@synthesize progressview;
@synthesize timer;
-(IBAction)Onclick{
progressview.progress = 0.0;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(update)
userInfo:nil
repeats:YES];
}
-(void) update{
progressview.progress +=0.1;
NSLog(@"%f",progressview.progress);
if (progressview.progress == 1.0) {
[timer invalidate];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc
{
[progressview release];
[super dealloc];
}
@end
第三步同样跟上面的等待控件一样,进行对界面输入输出。运行结果就是,进度条每一秒钟运行一次,10秒后完成