提示框(警告框)控件:UIAlertView

 
功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能。
 
类型:typedef NS_ENUM(NSInteger, UIAlertViewStyle) {

UIAlertViewStyleDefault = 0,                 //默认类型

UIAlertViewStyleSecureTextInput,          //安全密码的文本框输入类型

UIAlertViewStylePlainTextInput,            //普通文本框的文本框输入类型

UIAlertViewStyleLoginAndPasswordInput //登陆账号和密码输入类型

};

属性:

@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 ;    // 提示框类型
 
注意:如果按钮数量超出屏幕显示范围,则会创建类似tableView的效果。
对象方法:
 
 ※初始化提示框

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id<UIAlertViewDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, …);
 
※添加一个按钮,返回该按钮的索引值

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

※返回指定索引值的提示框标题

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

※显示提示框

-(void)show;

※点击指定的按钮时提示框消失

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

※设置输入文本框的索引,返回文本框

- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex

协议:代理方法

protocol UIAlertViewDelegate <NSObject>

@optional

※点击提示框上的按钮时触发的方法

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

※提示框将要消失时触发的方法

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

※提示框将要显示时触发的方法

- (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;

@end

具体的举例如下:

首先在视图控制器视图中创建按钮并添加事件,便于完成提示框的创建:

 - (void)viewDidLoad {
[super viewDidLoad]; //在视图中添加按钮事件
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(, , , )]; [button setTitle:@"点击" forState:UIControlStateNormal]; [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; button.center = self.view.center; //第一种形式:提示框上添加一两个按钮的默认类型
//[button addTarget:self action:@selector(Clicked_1:) forControlEvents:UIControlEventTouchUpInside]; //第二种形式:提示框上添加多个按钮的默认类型
//[button addTarget:self action:@selector(Clicked_2:) forControlEvents:UIControlEventTouchUpInside]; //第三种形式:提示框的类型为普通文本框输入类型
//[button addTarget:self action:@selector(Clicked_3:) forControlEvents:UIControlEventTouchUpInside]; //第四种形式:提示框的类型为安全文本框输入类型
//[button addTarget:self action:@selector(Clicked_4:) forControlEvents:UIControlEventTouchUpInside]; //第五种形式:提示框的类型为登陆账号和密码文本框输入类型
[button addTarget:self action:@selector(Clicked_5:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button];
}

1、第一种形式:提示框上添加一两个按钮的默认类型

 #pragma mark -Clicked1  提示框上添加一两个按钮的默认类型
-(void)Clicked_1:(UIButton*)sender
{
//创建提示框对象
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"信息输入有误" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil]; //显示提示框
[alertView show];
}

演示结果:

2、第二种形式:提示框上添加多个按钮的默认类型

 #pragma mark -Clicked2  提示框上添加多个按钮的默认类型
-(void)Clicked_2:(UIButton*)sender
{
//创建提示框对象
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"多个按钮" delegate:self cancelButtonTitle:@"button1" otherButtonTitles:@"button2", nil]; //继续添加按钮
[alertView addButtonWithTitle:@"button3"];
[alertView addButtonWithTitle:@"button4"];
[alertView addButtonWithTitle:@"button5"];
[alertView addButtonWithTitle:@"button6"];
[alertView addButtonWithTitle:@"button7"]; //显示提示框
[alertView show];
}

演示结果:

3、第三种形式:提示框的类型为普通文本框输入类型

 #pragma mark -Clicked_3  提示框的类型为普通文本框输入类型
-(void)Clicked_3:(UIButton*)sender
{
//创建提示框对象
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"请输入信息" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil]; alertView.alertViewStyle = UIAlertViewStylePlainTextInput; //显示提示框
[alertView show];
}

演示结果:

4、第四种形式:提示框的类型为安全文本框输入类型

 #pragma mark -Clicked_4  提示框的类型为安全文本框输入类型
-(void)Clicked_4:(UIButton*)sender
{
//创建提示框对象
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"请输入信息" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil]; alertView.alertViewStyle = UIAlertViewStyleSecureTextInput; //显示提示框
[alertView show];
}

演示结果:

5、第五种形式:提示框的类型为登陆账号和密码文本框输入类型

 #pragma mark -Clicked_5  提示框的类型为登陆账号和密码文本框输入类型
-(void)Clicked_5:(UIButton*)sender
{
//创建提示框对象
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"请输入账号和密码" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil]; alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput; //显示提示框
[alertView show];
}

演示结果:

协议代理的方法的使用:

1、首先实现协议:@interface ViewController ()<UIAlertViewDelegate>

2、其次设置代理,以上面的第一种形式提示框举例:

 #pragma mark -Clicked1  提示框上添加一两个按钮的默认类型
-(void)Clicked_1:(UIButton*)sender
{
//创建提示框对象
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示信息" message:@"信息输入有误" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:@"取消", nil]; alertView.delegate = self; //设置代理 //显示提示框
[alertView show];
}

3、实现协议的方法:

 #pragma mark -<UIAlertViewDelegate>
//※点击提示框上的按钮时触发的方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"alertView:clickedButtonAtIndex:");
} //※提示框将要消失时触发的方法
- (void)alertViewCancel:(UIAlertView *)alertView
{
NSLog(@"alertViewCancel:");
} //※提示框将要显示时触发的方法
- (void)willPresentAlertView:(UIAlertView *)alertView
{
NSLog(@"willPresentAlertView:");
} //※已经显示提示框时触发的方法
- (void)didPresentAlertView:(UIAlertView *)alertView
{
NSLog(@"didPresentAlertView:");
} //※提示框将要消失是触发的方法
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"alertView: willDismissWithButtonIndex:");
} //※提示框已经消失时触发的方法
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"alertView: didDismissWithButtonIndex:");
} //※设置提示框的第一个按钮是否不是取消按钮
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSLog(@"NO");
return NO;
}

刚开始时View中的按钮截图:

点击"点击"按钮时,截图中其他第一个按钮被设置为不是取消按钮(按钮无效)和代理方法执行顺序结果:

-- ::30.239 提示框(警告框)UIAlertView[:] willPresentAlertView:
-- ::30.240 提示框(警告框)UIAlertView[:] NO
-- ::30.763 提示框(警告框)UIAlertView[:] didPresentAlertView:

点击"确认"时,剩下的代理方法执行顺序的结果:

-- ::27.366 提示框(警告框)UIAlertView[:] alertView:clickedButtonAtIndex:
-- ::27.367 提示框(警告框)UIAlertView[:] alertView: willDismissWithButtonIndex:
-- ::27.777 提示框(警告框)UIAlertView[:] alertView: didDismissWithButtonIndex:

总的执行结果为:

-- ::30.239 提示框(警告框)UIAlertView[:] willPresentAlertView:
-- ::30.240 提示框(警告框)UIAlertView[:] NO
-- ::30.763 提示框(警告框)UIAlertView[:] didPresentAlertView:
-- ::27.366 提示框(警告框)UIAlertView[:] alertView:clickedButtonAtIndex:
-- ::27.367 提示框(警告框)UIAlertView[:] alertView: willDismissWithButtonIndex:
-- ::27.777 提示框(警告框)UIAlertView[:] alertView: didDismissWithButtonIndex:

iOS:提示框(警告框)控件UIAlertView的详解的更多相关文章

  1. 《手把手教你》系列技巧篇(三十八)-java+ selenium自动化测试-日历时间控件-下篇(详解教程)

    1.简介 理想很丰满现实很骨感,在应用selenium实现web自动化时,经常会遇到处理日期控件点击问题,手工很简单,可以一个个点击日期控件选择需要的日期,但自动化执行过程中,完全复制手工这样的操作就 ...

  2. iOS:提示框(警告框)控件UIActionSheet的详解

    提示框(警告框)控件2:UIActionSheet 功能:当点击按钮或标签等时,弹出一个提示框,显示必要的提示,然后通过添加的按钮完成需要的功能.它与导航栏类似,它继承自UIView.   风格类型: ...

  3. iOS:网页视图控件UIWebView的详解

    网页视图控件:UIWebView 功能:它是继承于UIView的,是一个内置的浏览器控件,以用来浏览从网络下载下来的网页或者本地上加载下来的文档. 枚举: //网页视图导航类型 typedef NS_ ...

  4. iOS:选择器控件UIPickerView的详解和演示

    选择器控件UIPickerView: 功能:它能够创建一个类似于密码锁式的单列或多列的选择菜单,用户可以通过它设置的代理来选择需要菜单中的任意的数据.例如创建日历.字体表(类型.大小.颜色).图库等. ...

  5. iOS:下拉刷新控件UIRefreshControl的详解

    下拉刷新控件:UIRefreshControl 1.具体类信息: @interface UIRefreshControl : UIControl //继承控制类 - (instancetype)ini ...

  6. iOS:图像选取器控制器控件UIImagePickerController的详解

    图像选择控制器:UIImagePickerController 功能:用于选取相册或相机等里面的照片. @interface UIImagePickerController : UINavigatio ...

  7. delphi控件属性大全-详解-简介

    http://blog.csdn.net/u011096030/article/details/18716713 button 组件: CAPTION 属性 :用于在按钮上显示文本内容 Cancel ...

  8. 【VB技巧】VB ListView 控件功能使用详解

    来源:http://lcx.cc/?i=494 ListView控件 在工具箱上击鼠标右键,选择快捷菜单的Components(部件)项,在控件列表中选择Microsoft Windows Commo ...

  9. 转 VB ListView控件各种操作详解

    Private Sub Form_Load() ListView1.ListItems.Clear '清空列表 ListView1.ColumnHeaders.Clear '清空列表头 ListVie ...

随机推荐

  1. 将内存图像数据封装成QImage V2

    转:http://www.cnblogs.com/bibei1234/p/3161555.html 如何将内存图像数据封装成QImage 当采用Qt开发相机数据采集软件时,势必会遇到采集内存图像并进行 ...

  2. python爬虫实战(六)--------新浪微博(爬取微博帐号所发内容,不爬取历史内容)

    相关代码已经修改调试成功----2017-4-13 详情代码请移步我的github:https://github.com/pujinxiao/sina_spider 一.说明 1.目标网址:新浪微博 ...

  3. BNUOJ 52505 Euclidean Geometry

    结论. 算了好久不会算,最后看了样例猜出了结论.次长边全用上,再用最长边减去次长边. #include<bits/stdc++.h> using namespace std; int T; ...

  4. golang中接口interface和struct结构类的分析

    再golang中,我们要充分理解interface和struct这两种数据类型.为此,我们需要优先理解type的作用. type是golang语言中定义数据类型的唯一关键字.对于type中的匿名成员和 ...

  5. Git in Powershell saying 'Could not find ssh-agent'

    加入系统环境变量D:\Program Files\Git\usr\bin

  6. 创建模态提醒窗口(UIAlertView)

    UIAlertView类创建一个简单的模态提醒窗口,可能包含消息.按钮以及文本框.模态UI元素要求用户必须与之交互(通常是按下按钮)后才能做其它事情.它们通常位于其他窗口前面,在可见时禁止用户与其他任 ...

  7. 【BZOJ 4180】 4180: 字符串计数 (SAM+二分+矩阵乘法)

    4180: 字符串计数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 164  Solved: 75 Description SD有一名神犇叫做Oxe ...

  8. luogu 11月月赛 斐波那契数列

    本来想作为水题刷,很快就想出了做法,结果细节实现太差改了好久... 根据题意你会发现其实就是求方程 ax+by=k解的个数. 此时 a=f[i],b=f[i+1],而(x,y)就是你要求的数对. 于是 ...

  9. 关于iis6.0远程溢出漏洞

    漏洞描述 漏洞编号:CVE-2017-7269 发现人员:Zhiniang Peng和Chen Wu(华南理工大学信息安全实验室,计算机科学与工程学院) 漏洞简述:开启WebDAV服务的IIS 6.0 ...

  10. Android Studio NDK开发浅谈

    环境: Android Studio 1.1.0 NDK-r10d 1.新建项目--->包名:com.mxl.az.ndk 新建包含native方法的类:JniOperation.class p ...