1.第一种方法创建
//创建警告栏 设置警告栏的代理对象为self 单个按钮
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您要取消订单吗? 亲" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//在第一个参数中设置标题
//message中设置具体警告信息
//cancelButtonTitle 取消按钮的名称
//otherButtonTitles 设置其他按钮的名称 如果有多个按钮名称之间用逗号隔开
//设置警告栏的样式 默认为UIAlertViewStyleDefault 其它的样式可以自己实验
alertView.alertViewStyle = UIAlertViewStyleDefault;
//显示警告栏 没有这个警告栏显示不出来
[alertView show];
2.第二种方法创建警告框 通过警告框的属性
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.title = @"温馨提示";
alertView.message = @"您要取消订单吗? 亲";
[alertView addButtonWithTitle:@"OK"];
[alertView show];
[self performSelector:@selector(dismissAlert:) withObject:alertView afterDelay:3.0];
- (void)dismissAlert:(UIAlertView *)alertView
{
if (alertView.visible)
{//visible 表示的是状态栏是否显示
[alertView dismissWithClickedButtonIndex:alertView.cancelButtonIndex animated:YES];
}
}
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.title = @"温馨提示";
alertView.message = @"您要取消订单吗? 亲";
//这种方法创建按钮要区别用构造方法创建按钮
[alertView addButtonWithTitle:@"OK"];
[alertView addButtonWithTitle:@"NO"];
//这里如果设置了0 那么 ok是cancelbutton 如果设置1 那么NO是cancelButton
//深色按钮为无害按钮 如取消按钮
//在两个按钮状态下 取消按钮在左边
alertView.cancelButtonIndex = 0;
[alertView show];
- (void)willPresentAlertView:(UIAlertView *)alertView
{
NSLog(@"警告框显示前被调用");
}
- (void)didPresentAlertView:(UIAlertView *)alertView
{
NSLog(@"警告框显示后被调用");
}
- (void)alertViewCancel:(UIAlertView *)alertView
{
NSLog(@"警告显示中强制关闭时被调用");
/*
例如警告框显示时应用程序突然关闭等场合
触摸取消按钮时不调用此方法
*/
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"触摸警告框中任意按钮时被调用");
//这个方法 - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex 先调用
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"警告框关闭前被调用");
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"警告框关闭之后被调用");
/*警告框显示中应用程序进入睡眠也会被调用*/
}