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) {

//....点击了有破坏性的那个危险按钮

}

}

效果如下图:

最新文章

  1. 理解Session与Cookie
  2. CSS3与页面布局学习笔记(六)——CSS3新特性(阴影、动画、渐变、变形( transform)、透明、伪元素等)
  3. Delphi Webbrowser 修改 textarea 值 百度
  4. Java基础之在窗口中绘图——绘制曲线(CurveApplet 1)
  5. 【开源java游戏框架libgdx专题】-02-Eclipse Gradle 环境安装
  6. python_爬校花图片
  7. sql注入中关于--+的一点探索
  8. C#中生成GUID的四种格式
  9. 在 Server 2008 企业版下, 安装 IIS 7 后,勾选好 请求筛选模块了。安装完毕后,&quot;请求筛选&quot;却不显示!
  10. jenkins安装Scanner插件
  11. Python3自定义日志类 mylog
  12. _itemmod_extra_equipments
  13. English trip V1 - B 13. Are you a model? 你是模特吗? Teacher:Patrick Key: 单词回顾、词性后缀
  14. MySQL子查询慢现象的解决
  15. 在websocket中怎么样注入service类
  16. docker-runc not installed on system 问题
  17. HTML实例(四)
  18. 一道hive面试题:explode map字段
  19. 第九篇:使用 AdaBoost 元算法提高分类器性能
  20. Redis在实际项目中的一应用场景

热门文章

  1. Objective-C中的单例模式(工具类)
  2. QML添加右键菜单
  3. 无法捕获的异常:MissingMethodException
  4. MyISAM 存储引擎
  5. Jasper_table_pass parameter to table component
  6. js 浮点数加减问题
  7. Keil C51处理可重入函数问题的探讨
  8. QtCreator调试传入运行参数
  9. 2015第15周日PostgreSQL学习
  10. Paint House 解答