UIAlertView笔记

来源:互联网 发布:淘宝卖家违规扣分 编辑:程序博客网 时间:2024/06/06 23:56

UIAlertView是创建一个用以给用户提示或者警告的提示框。能包含多个按钮。

UIAlertView是一个模态的(阻塞式),因此在应用中不应当频繁使用。一般应用场景:应用不能继续运行时;询问另外的解决方案;询问对操作的授权。

使用UIAlertView就需要实现UIAlertViewDelegate协议。这个协议是UIAlertView的委托协议。点击UIAlertView中的按钮会给委托对象发送alertView:clickedButtonAtIndex:消息。

相关实现代码如下:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"错误" message:@"用户名密码错误" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];     [alertView show];

在实例化UIAlertView对象时,最常用的方法是:initWithTitle:message:delegate:cancelButtonTitle:otherButtonTitles:。其中delegate可以设定为self,即将警告框委托给当前所在的控制器,cancelButtonTitle参数用于设置“取消”按钮的标题,实际显示中是左侧的按钮。otherButtonTitles是除取消按钮以外的全部,以字符串数组的形式传入,最后一个数组为nil。

虽然可以通过otherButtonTitles来设定多个按钮,但一般一个警告框最多设定两个,否则影响用户体验。

监听用户按了哪个按钮可以通过alertView:clickedButtonAtIndex:进行实现。传入参数为整型数字,cancelButton为0,otherButton从做往右依次为1,2,3.....

0 0
原创粉丝点击