iOS中UIAlertView警告框组件的使用教程

2020-01-15 15:04:09于丽
需要注意的是在IOS8之后,UIAlertView和UIActionSheet这两个控件被UIAlertController代替,但是这两个控件依然可以使,下面我们就简单了解一下iOS中UIAlertView警告框组件的使用教程  

1. 最简单的用法
初始化方法:

复制代码
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;
这个方法通过设置一个标题,内容,代理和一些按钮的标题创建警告框,代码示例如下:
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"我的警告框" message:@"这是一个警告框" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
效果如下:

 

iOS,UIAlertView,警告框

注意:如果按钮数超过两个,将会创建成如下样子:

iOS,UIAlertView,警告框

如果按钮数量超出屏幕显示范围,则会创建类似tableView的效果。

2. 为UIAlertView添加多个按钮