IOS中UIAlertView(警告框)常用方法总结

一、初始化方法

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString*)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;

这个方法通过设置一个标题,内容,代理和一些按钮的标题创建警告框,代码示例如下:

1
2
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"我的警告框" message:@"这是一个警告框" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];

效果如下:

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

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

二、属性与方法解析

标题属性

@property(nonatomic,copy) NSString *title;

内容属性

@property(nonatomic,copy) NSString *message;

添加一个按钮,返回的是此按钮的索引值

- (NSInteger)addButtonWithTitle:(NSString *)title;

返回根据按钮索引按钮标题

- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;

获取按钮数量

@property(nonatomic,readonly) NSInteger numberOfButtons;

设置将某一个按钮设置为取消按钮

@property(nonatomic) NSInteger cancelButtonIndex;

返回其他类型按钮第一个的索引值

@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;

警告框是否可见

@property(nonatomic,readonly,getter=isVisible) BOOL visible;

显现警告框

- (void)show;

代码模拟点击按钮消失触发方法

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

设置警告框风格

@property(nonatomic,assign) UIAlertViewStyle alertViewStyle;

风格的枚举如下

1
2
3
4
5
6
typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
    UIAlertViewStyleDefault = 0,//默认风格
    UIAlertViewStyleSecureTextInput,//密码输入框风格
    UIAlertViewStylePlainTextInput,//普通输入框风格
    UIAlertViewStyleLoginAndPasswordInput//账号密码框风格
};

这个方法设置文本输入框的索引

- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex;

三、UIAlertViewDelegate中的方法

点击按钮时触发的方法

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

将要展现警告框时触发的方法

- (void)willPresentAlertView:(UIAlertView *)alertView;

已经展现警告框时触发的方法

- (void)didPresentAlertView:(UIAlertView *)alertView;

警告框将要消失时触发的方法

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

警告框已经消失时触发的方法

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

设置是否允许第一个按钮不是取消按钮

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView;

辅助功能:UIAlertViewStyle的类型:

    UIAlertViewStyleDefault = 0:一个标准的警告框

UIAlertViewStyleSecureTextInput, :一个能够让用户输入文本的警告框,并且文本框是掩盖的

UIAlertViewStylePlainTextInput, :一个能够让用户 输入文本的警告框

UIAlertViewStyleLoginAndPasswordInput :一个能够让用户输入账号和密码的警告框

UIkit框架之UIalert(iOS 9之后就不用这个了)的更多相关文章

  1. iOS学习32之UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  2. iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。

    转发:http://my.oschina.net/u/1378445/blog/335014 iOS UIKit动力学 Dynamics UIAttachmentBehavior 实现iMessage ...

  3. iOS开发UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  4. 79、iOS 的Cocoa框架、Foundation框架以及UIKit框架

    Cocoa框架是iOS应用程序的基础 1. Cocoa是什么? Cocoa是 OS X和ios 操作系统的程序的运行环境. 是什么因素使一个程序成为Cocoa程序呢?不是编程语言,因为在Cocoa开发 ...

  5. 基础框架Fundation和UIkit框架的定义和使用

    Foundation 框架为所有应用程序提供基本的系统服务 您的应用程序以及 UIKit 和其他框架,都建立在 Foundation 框架的基础结构之上.Foundation 框架提供许多基本的对象类 ...

  6. ios UIKit动力 分类: ios技术 2015-07-14 12:55 196人阅读 评论(0) 收藏

    UIkit动力学是UIkit框架中模拟真实世界的一些特性. UIDynamicAnimator 主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. 它一般有两种初始 ...

  7. iOS-学习UIKIt框架的重要性

      前言: 众所周知,我们的移动设备的屏幕上可以展示很多图形界面,作为用户的我们可以通过屏幕上的图形界面浏览信息,也可以通过与图形界面的简单交互,在移动设备上实现各种各样的功能操作.....可以说,没 ...

  8. 让Playground支持UIKit框架

    http://blog.diveinedu.cn/playground_uikit_ios/ 让Playground支持UIKit框架 发表于 作者 排云鹤 — 暂无评论 ↓ Xcode 6新增了Pl ...

  9. Swift - 重写UIKit框架类的init初始化方法(以UITabBarController为例)

    原来写了篇文章讲UITabBarController的用法,当时是从UIViewController跳转到UITabBarController页面,代码如下: 1 self.presentViewCo ...

随机推荐

  1. 显示段落p中的前半部分内容 剩下的用三个点代替,点击更多时显示所有内容

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  2. Simplify Path [LeetCode]

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  3. cycleInterpolator 循环加速器

    CycleInterpolator Repeats the animation for a specified number of cycles. The rate of change follows ...

  4. ThoughtWorks微服务架构交流心得

      ThoughtWorks微服务架构交流心得: (1)<人月神话>中谈到软件开发没有银弹,根源在于软件所解决的领域问题本身固有的复杂性,微服务正是从领域问题角度上进行服务拆分,来降低软件 ...

  5. perl 正则匹配代码

    36     chomp $line; 37     my @vec = split /\t/, $line; 38     my @vec2 = ($vec[1]=~/[a-z]+/g); 39   ...

  6. eclipse 连接 mysql

    1.下载驱动. 2.eclipse->add extend jars -> 添加驱动. 3.测试: 在mysql 建立数据库和表,在eclipse 里对数据库进行操作. 代码: mysql ...

  7. S1:对象与JSON

    JSON全称为JavaScript对象表示法(JavaScript Object Notation). JSON是JavaScript中对象的字面量,是对象的表示方法,通过使用JSON,可以减少中间变 ...

  8. 转: HTTP协议的头信息详解

    通常HTTP消息包括客户机向服务器的请求消息和服务器向客户机的响应消息.这两种类型的消息由一个起始行,一个或者多个头域,一个只是头域结束的空行和可 选的消息体组成.HTTP的头域包括通用头,请求头,响 ...

  9. SO从 \u 这样的字符串 构建对象

    ShowMessage(SO('\u4F18\u8D28\u670D\u52A112').AsString); 正确 得到 优质服务12 ShowMessage(so( 个数字,后面的中文未能解析出.

  10. EditorLineEnds.ttr 错误问题

    安装 Windows Write Live,在线安装,会先安装一个什么补丁,中途提示失败. 运行Delphi2007,第一次成功,第二次就是 EditorLineEnds.ttr文件错误. http: ...