UIAlertView与UIActionSheet
1.UIAlertView(警告框)
1.1 创建警告框,设置样式
- (IBAction)alertView:(UIButton *)sender {//创建button按钮
//创建警告框的实例
//UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"提示信息message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
//如有多个otherButtonTitles,先显示otherButtonTitles。otherButtonTitles有一个,cancelButtonTitle在左边
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"提示信息message" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES",nil];
//显示警告框
[alert show];//系统自带show方法
}
效果如下图:
1.2 创建有提示信息输入的警告框
- (IBAction)alertViewInput:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"标题" message:@"输入您的用户信息" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//设置警告框的样式
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
//显示警告框
[alert show];
}
效果如下图:
1.3创建有提示信息输入的警告框
UIAlertViewStyleDefault
UIAlertViewStyleSecureTextInput,//单行密码形式
UIAlertViewStylePlainTextInput, //单行正常形式
UIAlertViewStyleLoginAndPasswordInput//用户名和密码
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
1.4 如何获取用户在警告框上的选择——委托
a)什么是委托:
一个对象(对象A)让另一个对象(对象B)帮它做事情(发消息)。
b)委托协议:
是委托方要求代理方所符合的规则,这些规则对应的就是一组方法,并且每个方法的第一个参数都是委托方的引用,每个方法的名称用于描述方法的执行时机
c)设置控制器实例成为alertView的代理方的步骤
1)控制器遵守协议//委托方定义协议的名称要求"委托方name+Delegate"
2)控制器实现协议中的方法//要想成为代理方,必须满足委托方定义的协议
//第一个参数永远是委托方的引用,点进协议直接复制
3)在创建UIAlertView时设定控制器实例为代理方法
例:通过输入用户名和密码信息,点击按钮。取出用户名和密码
/*1.设置控制器成为警告框的代理,需要控制器(代理方)遵守协议*/
@interface AlertViewController ()<UIAlertViewDelegate>
@end
@implementation AlertViewController
- (IBAction)alertViewDelegate:(id)sender {
//3.设置了警告框的代理方为当前控制器实例,于是,当用户点击了警告框上的某个按钮时该动作的处理就交给了控制器实例来响应
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"title" message:@"your choice" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES",nil]; //self
alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
[alert show];
}
/*2.设置控制器成为警告框的代理,实现协议中的方法选择哪个方法实现,根据不同的方法对应的执行时机可以从方法名判断发消息的时机。
方法的第一个参数一定是委托方的引用
*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//目标:当点击YES按钮时,在控制器打印输入的用户名和密码
if (alertView.cancelButtonIndex !=buttonIndex) {
//获取用户名
NSString *loginName =[alertView textFieldAtIndex:0].text;
//获取密码
NSString *pwd =[alertView textFieldAtIndex:1].text;
NSLog(@"用户名:%@ ,密码:%@",loginName,pwd);
}
}
结果:
用户名:yang ,密码:123
1.5 如何区分用户点击的按钮
在 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex方法中,第二个参数为点击的按钮的索引,可以使用以下几种方法进行判断
方法一:直接判断索引值区分不同的按钮
方法二:根据索引值获取按钮的title,进行区分
方法三:判断索引是否是cancelButtonIndex进行区分
//根据点击的按钮的索引获取点击的按钮的标题
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
方法1:判断标题来区分不同的动作
if ([title isEqualToString:@"YES"]) {
// NSLog(@"点击了YES按钮");//根据点击了按钮OK执行的动作 }else{
// NSLog(@"点击了NO按钮");
}
方法3:也可以通过判断是不是cancel按钮的索引
//来判断是否点击了cancel按钮
if (alertView.cancelButtonIndex == buttonIndex)
{
// NSLog(@"点击了NO按钮");
}
}
1.6 如何获取alertView中输入框内的文本
利用alertView的textFieldAtIndex:方法,获得不同索引位置上的文本框,然后反问text属性即可
NSString *pwd = [alertView textFieldAtIndex:1].text;
2.UIActionSheet(操作表)
2.1 创建操作表
//创建actionSheet的实例
UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"QQ空间" otherButtonTitles:@"微博",@"微信",@"朋友圈", nil];
//显示actionSheet
[sheet showInView:self.view];
2.2 判断用户点击的不同的按钮
a)需要控制器实例遵守协议
b)需要控制器实现协议中的方法
#import "ActionSheetViewController.h"
@interface ActionSheetViewController ()<UIActionSheetDelegate>//遵守协议
@end
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//区分点击的按钮
//NSLog(@"%d",buttonIndex);//获取按钮标题对应的值
//获取按钮上的标题
//NSLog(@"%@",[actionSheet buttonTitleAtIndex:buttonIndex]);
if (actionSheet.cancelButtonIndex == buttonIndex) {
//....点击了cancel
}
if (actionSheet.destructiveButtonIndex == buttonIndex) {
//....点击了有破坏性的那个危险按钮
}
}
效果如下图:
UIAlertView与UIActionSheet的更多相关文章
- UIAlertView、 UIActionSheet
一.UIAlertView. UIActionSheet都是ios系统自带的弹出式对话框,当UIAlertView或UIActionSheet弹出来时用户无法与应用界面中的其它控件交互,UIAlert ...
- iOS开发——UI篇Swift篇&UIAlertView/UIActionSheet
UIAlertView/UIActionSheet UIAlertView //一个按钮的提醒 @IBAction func oneButtonAler() { //创建单一按钮提醒视图 let on ...
- UIAlertView、UIActionSheet兼容iOS8
链接地址:http://blog.csdn.net/nextstudio/article/details/39959895?utm_source=tuicool 1.前言 iOS8新增了UIAlert ...
- 用block将UIAlertView与UIActionSheet统一起来
用block将UIAlertView与UIActionSheet统一起来 效果 1. 将代理方法的实例对象方法转换成了类方法使用 2. 要注意单例block不要长期持有,用完就释放掉 源码 https ...
- iOS8以后UIAlertView和UIActionSheet两种alert页面都将通过UIAlertController来创建
1. Important: UIAlertView is deprecated in iOS 8. (Note that UIAlertViewDelegate is also deprecated. ...
- UIAlertView及UIActionSheet 在ios8极其以下版本的兼容问题解决方案
本文转载至 http://www.aichengxu.com/view/35326 UIAlertView及UIActionSheet在ios8中被放弃,其功能将完全由UIAlertControlle ...
- iOS 8 中 UIAlertView 和 UIActionSheet 河里去了?
iOS 8 中 UIAlertView 和 UIActionSheet 河里去了? 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商 ...
- iOS:简单使用UIAlertVIew和UIActionSheet
做iOS开发的同学想必都用过UIAlertVIew或者UIActionSheet.UIAlertVIew 可以弹出一个出现在屏幕中间的提示视图,给用户展示信息,并让用户自己选择操作,UIActionS ...
- UIAlertView 与 UIActionSheet (提示用户)的使用方法
UIAlertView 提示用户 帮助用户选择框 // UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"警 ...
- iOS - 提示信息 - UIAlertView、UIActionSheet、UIAlertController的实际应用
1.UIAlertView(屏幕中央弹出框)(不需要服从代理) UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@" ...
随机推荐
- 网站飘窗js代码
<SCRIPT> var imagepath="/${res}/images/geren.jpg" ; var imagewidth=178 ;//这两行写图片的大小 ...
- 将实体转成XML,XML节点顺序由我控制
一.前言 由于有时候返回xml格式比较严格,需要按照一定的顺序排列节点才能够符合要求,这里主要用到了自定义一个List<string> 字符顺序,再让实体属性按照List定义好的顺序重新排 ...
- POJ - 2653 - Pick-up sticks 线段与线段相交
判断线段与线段相交 莫名其妙的数据量 #include <iostream> #include <cstdio> #include <vector> #includ ...
- HBase -ROOT-和.META.表结构(region定位原理)
在HBase中,大部分的操作都是在RegionServer完成的,Client端想要插入,删除,查询数据都需要先找到相应的RegionServer.什么叫相应的RegionServer?就是管理你要操 ...
- php 生成和下载xml文件
方法一 $doc = new DOMDocument('1.0', 'utf-8'); $doc->formatOutput = true; $rootEle = $doc->create ...
- Android再学习-20141018-布局-进度条
20141018-Android再学习 对齐至控件的基准线 为了保证印刷字母的整齐而划定的线(四线三格的第三条线). android:layout_alignBaseline 与父控件的四个边缘对齐( ...
- MathJax测试
\begin{array}{cc} a & b \\ c & d \end{array} \begin{equation} \int_0^\infty \frac{x^3}{e^x-1 ...
- KMP算法总♂结
讲KM♂P算法之前,我们先讲一个故♂事. 有一天,sgg给了老obo一封信和一个单词,并给他一个任务:找出这封信出现了多少个单词,然后在规定时间内告诉他. 碰到这个问题,老obo会怎么做呢? 首先最直 ...
- Sql server中Collation conflict问题
SQL语句查询时select A.Code,A.Name,a.Systemcode,B.ID,B.LogType,B.DMCode,B.IP,B.Department,B.CreateBy,B.Cre ...
- SendMessage参数
http://download.csdn.net/download/wshjldaxiong/4830242