·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> UIAlertView----警告视图

UIAlertView----警告视图

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

UIAlertView是一个简单的类,它的作用就是对弹出的警告框进行设置

一、首先给大家看一下它的形成代码和效果图

   1、将tableView中的cell属性进行一下简单的设置:

  在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;//返回每个单元格中显示内容的View

  将cell的accessoryType属性设为UITableViewCellAccessoryDetailDisclosureButton或者是UITableViewCellAccessoryDetailButton

  e.g.  cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton ;

  2、//点击触发的方法
  -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
  {
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了辅助按钮" delegate:nil cancelButtonTitle:@"不好"  otherButtonTitles:@"好", nil]; 

  alert.alertViewStyle = UIAlertViewStyleDefault ; //默认模式
      [alert show];
  }

3、效果图

                                                           

 是不是感觉很熟悉,下面这个效果见到的时候也很多。

                                                          

从上面可以看到实现它的代码不是很多,主要的还是熟悉它们的属性以及方法,这样就可以展示出我们想要的结果

二、属性以及方法

1、属性
  @PRoperty(nonatomic,assign) id /*<UIAlertViewDelegate>*/ delegate; 代理属性
  @property(nonatomic,copy) NSString *title; //警告的名字(显示在第一行的东东)
  @property(nonatomic,copy) NSString *message;   //警告信息(显示在第二行的东东)
  @property(nonatomic,readonly) NSInteger numberOfButtons; //按钮的个数
  @property(nonatomic) NSInteger cancelButtonIndex;     //删除哪一个按钮
  @property(nonatomic,readonly) NSInteger firstOtherButtonIndex;    //
  @property(nonatomic,readonly,getter=isVisible) BOOL visible; //是否可用
  @property(nonatomic,assign) UIAlertViewStyle alertViewStyle //警告视图格式
     UIAlertViewStyle有四种格式: (上面的第二张图就是由它改变而来的,大家试下其他的属性)
     UIAlertViewStyleDefault  //默认的格式,没有添加其他东西
     UIAlertViewStyleSecureTextInput, //出现一个textField供用户输入
     UIAlertViewStylePlainTextInput, //出现一个textField供用户输入

     UIAlertViewStyleLoginAndPassWordInput  //密码登陆模式,需要输入用户名和密码

2、初始化方法
  - (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString     *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
3、添加按钮 —从中间由右向左添加
  - (NSInteger)addButtonWithTitle:(NSString *)title;    // returns index of button. 0 based.
  - (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
4、显示警告 —要想出现警告提示就一定要有这个方法
  - (void)show;
5、移除按钮
  - (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

  上面用到了UIAlertViewDelegate这个代理方法,这里就不介绍了。

      如有错误,欢迎批评指正,谢谢。