我们在使用app的时候经常都会遇到各种各样的弹框,今天介绍一下系统的两种弹框
第一种,AlertView,就是普通的弹框
来看代码
//由于下面设置了代理(后面再说),所以我们要实现协议
@interface ViewController ()
@end
- (void)alert1{
self.view.backgroundColor = [UIColor purpleColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 50, 100, 100);
[btn setTitle:@"改变颜色" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
btn.tag = 10;
[self.view addSubview:btn];
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.frame = CGRectMake(170, 50, 100, 100);
[btn1 setTitle:@"改变透明度" forState:UIControlStateNormal];
[btn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
btn1.tag = 20;
[self.view addSubview:btn1];
}
- (void)btnClick:(UIButton *)btn{
switch (btn.tag) {
case 10:
{
// alert 警告
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"改变背景颜色" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"changeColor", nil];
alertView.tag = 10010;
// show 展示
[alertView show];
}
break;
case 20:
{
// alert 警告
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"改变透明度" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"changeAlpha", nil];
alertView.tag = 10086;
// show 展示
[alertView show];
}
break;
default:
break;
}
}
//传递当前类对象做为代理 就可以去使用系统的协议方法 或者代理方法
//两个参数 1.用来区分对象 2.区分按钮
//协议方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// 改变颜色
if (alertView.tag== 10010){
if (buttonIndex == 1){
self.view.backgroundColor = [UIColor yellowColor];
}
}
// 改变透明度
if (alertView.tag == 10086) {
if (buttonIndex == 1) {
self.view.alpha = 0.5;
}
}
NSLog(@"button index = %ld",buttonIndex);
}
第二种, ActionSheet
来看代码
//由于下面设置了代理(后面再说),所以我们要实现协议
@interface ViewController ()
@end
- (void)alert2{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(50, 50, 50, 50);
[btn setBackgroundColor:[UIColor redColor]];
[btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
// 获得屏幕上的所有子视图
array = [self.view subviews];
}
- (void)btnClick{
// destructive 破坏
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"ActionSheet" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"destructive" otherButtonTitles:@"ok", nil];
[actionSheet showInView:self.view];
}
//协议方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0){
// 遍历所有的子视图
for (UIView *view in array){
// 判断对象是否为UITextField的类型
if ([view isKindOfClass:[UITextField class]]){
[view removeFromSuperview];
}
}
}
}
然而很是不幸,虽然你学会了上面的两种弹框,但是你却不能用了,因为他们已经被苹果标记过时了,以后就变成这样写了,不过下面这种包含了上面那两种
- (void)alert{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(20, 64, 280, 50);
[btn setBackgroundColor:[UIColor greenColor]];
[btn addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)buttonClick{
UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"确定按钮");
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消按钮");
}];
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"警告" message:@"这是一个警告" preferredStyle:UIAlertControllerStyleActionSheet]; //这里可以选择样式,其实就是下面那两种弹框的样式
[alertC addAction:sureAction];
[alertC addAction:cancelAction];
[self presentViewController:alertC animated:YES completion:nil];
}