1、虽然UIAlertView弹窗方式在IOS9.0中已经不怎么使用,但是依然在这边列举一下。其实实现方式特别简单
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"测试弹窗" message:@"UIAlertView此弹窗方式已经淘汰" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
// 显示弹窗
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (0 == buttonIndex) {
NSLog(@"点击事件测试");
}
}
2、UIActionSheet底部弹出测试,此创建方式也已经不再推荐使用
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
UIActionSheet *sheet =[[UIActionSheet alloc] initWithTitle:@"UIActionSheet底部弹出测试" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil] ;
[sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (0 == buttonIndex) {
NSLog(@"UIActionSheet此种弹窗方式也已经取消");
}
}
3、UIAlertController是苹果官方推荐的创建弹窗的方式,先创建类似UIAlertView的弹窗
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
// UIAlertControllerStyleActionSheet = 0,// 底部弹窗
// UIAlertControllerStyleAlert// 中部弹窗
// }
// typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
// UIAlertActionStyleDefault = 0, // 使用默认方式创建
// UIAlertActionStyleCancel,// 取消操作,不对数据做任何改变的时候使用
// UIAlertActionStyleDestructive// 当删除或者修改数据的时候使用这个style
// }
// 中部弹窗测试
UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"中部弹窗测试" message:@"你想要的都在这里" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertView addAction:destructive];
[alertView addAction:cancel];
[self presentViewController:alertView animated:YES completion:nil];
}
4、然后再创建类似UIActionSheet的弹窗
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 1.底部弹窗测试(即以前的ActionSheet)
UIAlertController *logout = [UIAlertController alertControllerWithTitle:@"你确定注销?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"注销" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
[self.navigationController popViewControllerAnimated:YES];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
return;
}];
[logout addAction:destructive];
[logout addAction:cancel];
[self presentViewController:logout animated:YES completion:nil];
}
效果展示: