/*alertView.h*/
#import <UIKit/UIKit.h>
@interface alertView : UIViewController<UIAlertViewDelegate>
{
//创建控件对象
UIAlertView *iToast;
}
@property(nonatomic,retain) UIAlertView *iToast;
//让警告框消失的方法
-(void) dissmiss:(NSTimer*)timer;
@end
初始化对象,并添加点击事件
- (void)viewDidLoad
{
[super viewDidLoad];
//创建警告框
iToast = [[UIAlertView alloc]initWithTitle:@"警告" message:@"用户名或密码不正确,请重新输入" delegate:self cancelButtonTitle:@"Edit" otherButtonTitles:nil];
/*message:警告的内容
delegate: self是说由当前对象来处理UIAlertView对象所发生的事件,如果不需要则为空 nil
cancelButtonTitle:添加按钮 如果只有一个按钮把按钮的title写在这个属性中,后面的那个属性则为空 nil
otherButtonTitles:添加其它按钮 如果有多个按钮,在这个属性里写除第一个按钮外的所有按钮,不管有多少格按钮,最后都要写上 nil
例如: otherButtonTitles:@"ok",@"no",nil
*/
//当不进行操作,5秒后警告框会自动消失
[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(dissmiss:) userInfo:nil repeats:YES];
[iToast show];//显示
}
//给警告框添加点击事件,可以根据索引值来判断点击的是哪个按钮
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//打印所点击的按钮的索引
NSLog(@"buttonIndex = %d",buttonIndex);
}
//封装方法
-(void) dissmiss:(NSTimer*)timer
{
//dismissWithClickedButtonIndex 设置默认点击的按钮
[iToast dismissWithClickedButtonIndex:0 animated:NO];
}
-(void)dealloc
{
//释放
[iToast release];
[super dealloc];
}