iOS UIAlertView

iOS 开发中经常会遇到需要弹窗提示的情况(这个再浏览器端我们经常会遇到)

如下图:


这个是怎么实现的了?

其实很简单iOS提供一个类  UIAlertView

我们使用他即可

 UIAlertView *alertV=[[UIAlertView alloc]initWithTitle:@"alert标题" message:@"alertMesaage" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alertV show];

快去试试看吧


下来我们看下最后一个参数,ohterButtonTitles 他是可以输入多个值的,比如:

    UIAlertView *alertV=[[UIAlertView alloc]initWithTitle:@"alert标题" message:@"alertMesaage" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"第一次",@"第二次",@"第三次", nil];
    [alertV show];

那么他出来的效果如下:


界面是现实出来 但是最红我们还是要使用他的,问题就来了 我们点击取消 还是确定 

这个时候我们回头看刚才的方法里面 有个Delegate 我们设置的self  那么是不是又用到代理了

是的,万能的代理又出现了 

我们需要实现一个协议UIAlertViewDelegate

我们实现一个方法用来监测 alert 点击了哪个选项

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;


此时我们需要给当前类添加协议  

@interface ViewController ()<UIAlertViewDelegate>

我们以第个样式为例实现协议的方法

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:{
            
            NSLog(@"取消");
        }
            break;
        case 1:{
            NSLog(@"第一次");
        }
            break;
        case 2:{
            NSLog(@"第二次");
        }
            break;
        case 3:{
            NSLog(@"第三次");
        }
            break;
            
        default:
            break;
    }
}


好了 基本完成




苹果开发群 :414319235  欢迎加入  欢迎讨论问题





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值