IOS成长之路-UIAlertView控件


/*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];
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值