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:@" ...
随机推荐
- mongodb数据文件内部结构
有人在Quora上提问:MongoDB数据文件内部的组织结构是什么样的.随后10gen的工程师Jared Rosoff出来做了简短的回答. 每一个数据库都有自己独立的文件.如果你开启了director ...
- 《转载》深入理解 cocos2d-x 坐标系
原文地址:http://www.cnblogs.com/lyout/p/3292702.html. 首先我们添加两个测试精灵(宽:27,高:40)到场景里面: CCSprite *sprite1 = ...
- utf-8的mysql表笔记
链接数据库指定编码集jdbc:mysql://192.168.2.33:3306/mybase?useUnicode=true&characterEncoding=UTF-8 mysql默认链 ...
- activiti任务TASK
一.概要 设计TASK的表主要是:ACT_RU_TASK,ACT_HI_TASKINST(见参考-activiti表): 任务主要有:人工任务(usertask),服务任务(servicetask)等 ...
- mysql 命令行 自动补全
配置文件中 默认关闭自动补全: [mysql] #no-auto-rehash # faster start of mysql but no tab completition 改为: [mysql] ...
- LeetCode_Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /* ...
- 导入Excel表格到SharePoint站点
在Excel中创建表 选中所有要导入的行,点击菜单"插入"->"表格" 如果数据内容包含表头,选择"我的表格包含表头". 选择菜单 ...
- cocos2d-x特效之CCControlPotentiometer
在test示例下面,有一个关于此功能的代码,实现的效果如下: 通过拉动可旋转的按钮,从而改变所代表的值,这个效果的确是很棒的,但,和我的需求有一些差别,先贴上我实现的效果吧 ...
- hdr(host), hdr_beg(host) , path_beg
ACL derivatives : hdr([<name>[,<occ>]]) : exact string match 字符串精确匹配 hdr_beg([<name&g ...
- OpenWrt opkg 在线源默认配置
dest root /dest ram /tmplists_dir ext /var/opkg-listsoption overlay_root /overlaysrc/gz barrier_brea ...