本节学习内容:
1.警告对话框和等待提示器的概念
2.警告对话框和等待提示器的属性
3.警告对话框和等待提示器的使用
#import
@interface ViewController:UIViewController{
//定义一个警告对话框视图对象
UIAlertView* _alertView;
//等待提示对象,当下开载或加载比较大的文件时,可以显示此控件,处于提示等待状态
UIActivityIndicatorView* _activityIndicator;
}
@property(retain,nonatiomic) UIAlerView* alertView;
@property(retain,nonatiomic)UIActivityIndicatorView* activityIndicatorView;
@end
#import"ViewController.h";
@interface viewController()
@end
@implementation ViewController
//实现和成员变理的同步
@sysnthesize alertView=_alertView;
@sysnthesize activityIndicatorView=_activityIndicatorView;
-(void)viewdidLoad{
[super viewDidLoad];
for(int i=0;i<2;i++){
UIButton* btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.fram=CGRectMake(100,100+100*i,100,40);
if(i==0){
[btn setTitle:@"警告对话框"forState:UIControlStateNormal];
}
else if(i==1){
[btn setTitle:@"" forState:UIControlStateNormal];
}
btn.tage=101+i;
[btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
}
[self.view addSubview:btn];
}
-(void)pressBtn:(UIButton*)btn{
//警告对话框创建
if(btn.tag==101){
//创建警告对话框,参数1:对话框标题,参数2:提示信息,参数3:处理按扭事件的代理对象,参数4:取消按钮,参数5:其它按钮文字,参数6:...添加其它按钮,参数结束:表示按钮添加结束 nil;
_alertView=[[UIAlertView alloc]initWithTitle:@"警告" message:@"你的手机电量过低,即将关机,请保存好数据!" deleage:nil cancelButtonTitle:@"取消" otherButtonTitles:@"OK",@"12",nil];
//显示对话框
[_alertView show];
}
////创建等待提示器
else if(btn.tag==102){
_activityIndicator={UIActivityIndicatorView alloc] initWithFrame:CGRectMake(100,300,80,80)];
//设定提示的风格:小灰(Gray),小白(White),大白(WhiteLarge)
_activityIndicator.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
[self.view addSubview:_activityIndicator];
//自动动画并显示
[_activityIndicator startAnimating];
//停止等待动画并隐藏
//[_activityIndicator stopAnimationg];
}
}
}
//当点击对话框的按钮时,调用此函数,参数1:对话本身对象,参数2:按钮索引
//取消的索引值为0
-(void)alertView:(UIAlertview *)alertView ClickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(@"index=%ld\n",buttonIndex);
}
//对话框即将消失,调用此函数
-(void) alertView:(IAlertview *)alerView villDismissWithButtonIndex:(NSnteger)buttonIndex{
NSLog(@"即将消失!");
}
//对话框已经消失,调用此函数
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
}