UIAlertView、 UIActionSheet

警告框控件 (UIAlertView)

1. 警告框控件 (UIAlertView) 简介

(1) UIAlertView 创建流程


UIAlertView 创建流程 

-- 创建 UIAlertView : 创建时指定 标题, 内容, 按钮等信息, 按钮监听需要创建 UIAlertView 的 UIAlertViewDelegate 委托对象;

-- 显示 UIAlertView : 调用显示 UIAlertView 的显示方法;

-- 监听按钮 : 为委托对象实现 UIAlertViewDelegate 协议中的方法即可;

(2) UIAlertViewDelegate 协议方法

UIAlertViewDelegate 协议方法简介 : 

-- "- (void) alertView : (UIAlertView *) alertView clickedButtonAtIndex : (NSInteger) buttonIndex :" 方法 : 用户单击对话框中的按钮激发的方法, buttonIndex 是点击的按钮的索引;

-- "- (void) willPresentAlertView : (UIAlertView *) alertView" 方法 : 对话框将要显示时激发该方法;

-- "- (void) didPresentAlertView : (UIAlertView *) alertView" 方法 : 对话框完全显示出来后激发该方法;

-- "- (BOOL) alertViewShouldEnableFirstOtherButton : (UIAlertView *) alertView" 方法 : 对话框中除 cancel 按钮之后的第一个按钮被启用回调该方法;

-- "- (void) alertView : (UIAlertView *) alertView willDissmissWithButtonIndex : (NSInteger) buttonIndex" 方法: 单击某按钮将要隐藏警告框时激发该方法;

-- "- (void) alertView : (UIAlertView *) alertView didDissmissWithButtonIndex : (NSInteger) buttonIndex" 方法 : 单击某个按钮已经隐藏警告框后激发该方法;

-- "- (void) alertViewCancel : (UIAlertView * ) alertView " 方法 : 对话框被取消时激发的方法;

(3) UIAlertView 输入框风格设置

UIAlertView 的 actionSheetStyle 属性 : 

-- 主要作用 : 设置 UIAlertView 的风格, 取值是 枚举值;

-- UIAlertViewStyleDefault 枚举值 : 默认警告框风格;

-- UIAlertViewStyleSecureTextInput 枚举值 : 警告框中有一个密码输入框;

-- UIAlertViewStylePlainTextInput 枚举值 : 警告框中包含普通输入框;

-- UIAlertViewStyleLoginAndPasswordInput 枚举值 : 警告框中包含 用户名 密码输入;

访问输入框方法 : 

-- "- (UITextField *) textFieldAtIndex : (NSInteger) textFieldIndex" : 获取 索引值 为 textFieldIndex 的文本输入框;

2. 简单的对话框示例

(1) 创建 UIAlertView API

 

创建方法 : 

[objc] view plaincopy
 
  1. [[UIAlertView alloc] initWithTitle:<#(NSString *)#> message:<#(NSString *)#> delegate:<#(id)#> cancelButtonTitle:<#(NSString *)#> otherButtonTitles:<#(NSString *), ...#>, nil nil];  

-- 参数一 : initWithTittle 对话框名称;

-- 参数二 : message 对话框内容;

-- 参数三 : delegate 委托对象;

-- 参数四 : cancelButtonTittle 取消按钮文字内容;

-- 参数五 : otherButtonTittles 其它按钮文字内容;

-- 真实代码 : 

[objc] view plaincopy
 
  1. /* 
  2.     创建 UIAlertView 控件, 传入参数 标题 内容 委托对象 取消按钮 其它按钮 
  3.  */  
  4. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"对话框标题" message:@"对话框内容" delegate:self cancelButtonTitle:@"取消显示" otherButtonTitles:@"按钮1", @"按钮2", @"按钮3", @"按钮4", nil nil];  


显示对话框 : [UIAlertView show];

(2) 代码示例

代码示例 : 

-- 界面设计文件 : 

-- OCViewController.h : 

[objc] view plaincopy
 
  1. //  
  2. //  OCViewController.h  
  3. //  UIAlertView  
  4. //  
  5. //  Created by octopus on 15-12-14.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface OCViewController : UIViewController <UIAlertViewDelegate>  
  12. //按钮的 IBAction 方法  
  13. - (IBAction)click:(id)sender;  
  14.   
  15. @end  



-- OCViewController.m : 

[objc] view plaincopy
 
  1. //  
  2. //  OCViewController.m  
  3. //  UIAlertView  
  4. //  
  5. //  Created by octopus on 15-12-14.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import "OCViewController.h"  
  10.   
  11. @interface OCViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation OCViewController  
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.     // Do any additional setup after loading the view, typically from a nib.  
  21.       
  22. }  
  23.   
  24. - (void)didReceiveMemoryWarning  
  25. {  
  26.     [super didReceiveMemoryWarning];  
  27.     // Dispose of any resources that can be recreated.  
  28.       
  29. }  
  30.   
  31. //点击按钮弹出 UIAlertView 对话框  
  32. - (IBAction)click:(id)sender {  
  33.     /* 
  34.         创建 UIAlertView 控件, 传入参数 标题 内容 委托对象 取消按钮 其它按钮 
  35.      */  
  36.     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"对话框标题" message:@"对话框内容" delegate:self cancelButtonTitle:@"取消显示" otherButtonTitles:@"按钮1", @"按钮2", @"按钮3", @"按钮4", nil nil];  
  37.     //调用该方法显示 UIAlertView 控件  
  38.     [alert show];  
  39. }  
  40.   
  41. //实现的 UIAlertViewDelegate 协议中的方法  
  42. - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{  
  43.     NSString * msg = [NSString stringWithFormat:@"点击了按钮 %d", buttonIndex];  
  44.     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"弹出框" message: msg delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil nil];  
  45.     [alert show];  
  46.       
  47.       
  48. }  
  49. @end  



-- 运行界面展示 : 


3. 警告框控件 (UIAlertView) 示例代码

(1) 相关 API 简介 

相关 API 简介 : 

-- 设置 警告提示框 风格 : 

[objc] view plaincopy
 
  1. //设置提示框的风格 账号密码输入  
  2. alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;  


-- 设置输入框键盘输入类型 : 

[objc] view plaincopy
 
  1. //设置密码输入是数字键盘  
  2. [alertView textFieldAtIndex:1].keyboardType = UIKeyboardTypeNumberPad;  


-- 获取指定索引的输入框 

[objc] view plaincopy
 
  1. //获取账号输入文本框  
  2. UITextField * userNameField = [alertView textFieldAtIndex:0];  


-- 生成警告提示框 : 

[objc] view plaincopy
 
  1. //创建一个带 两个按钮的 提示框 确定 取消  
  2. UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"登录" message:@"输入用户名密码" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];  


-- 显示警告提示框 : 

[objc] view plaincopy
 
  1. //显示警告提示框  
  2. [alertView show];  



(2) 示例代码

示例代码 : 

-- 界面设计文件 : 

-- OCViewController.h : 

[objc] view plaincopy
 
  1. //  
  2. //  OCViewController.h  
  3. //  TextFieldUIAlertView  
  4. //  
  5. //  Created by octopus on 15-12-17.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface OCViewController : UIViewController <UIAlertViewDelegate>  
  12. - (IBAction)click:(id)sender;  
  13.   
  14. @end  



-- OCViewController.m : 

[objc] view plaincopy
 
  1. //  
  2. //  OCViewController.m  
  3. //  TextFieldUIAlertView  
  4. //  
  5. //  Created by octopus on 15-12-17.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import "OCViewController.h"  
  10.   
  11. @interface OCViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation OCViewController  
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.     // Do any additional setup after loading the view, typically from a nib.  
  21. }  
  22.   
  23. - (void)didReceiveMemoryWarning  
  24. {  
  25.     [super didReceiveMemoryWarning];  
  26.     // Dispose of any resources that can be recreated.  
  27. }  
  28.   
  29. - (IBAction)click:(id)sender {  
  30.     //创建一个带 两个按钮的 提示框 确定 取消  
  31.     UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"登录" message:@"输入用户名密码" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];  
  32.       
  33.     //设置提示框的风格 账号密码输入  
  34.     alertView.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;  
  35.     //设置密码输入是数字键盘  
  36.     [alertView textFieldAtIndex:1].keyboardType = UIKeyboardTypeNumberPad;  
  37.     //显示警告提示框  
  38.     [alertView show];  
  39. }  
  40.   
  41. - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{  
  42.     if(buttonIndex == 1){  
  43.         //获取账号输入文本框  
  44.         UITextField * userNameField = [alertView textFieldAtIndex:0];  
  45.         //获取密码输入文本框  
  46.         UITextField * passwordField = [alertView textFieldAtIndex:1];  
  47.         //生成 一个 包含账号 密码 输入的字符串  
  48.         NSString * content = [NSString stringWithFormat:@"用户名为 : %@, 密码为 : %@", userNameField.text, passwordField.text];  
  49.         //生成警告提示框  
  50.         UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"标题" message:content delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil nil];  
  51.         //显示警告提示框  
  52.         [alertView show];  
  53.     }  
  54.       
  55. }  
  56.   
  57. - (void) willPresentAlertView:(UIAlertView *)alertView{  
  58.     //遍历所有的 UIView 集合  
  59.     for(UIView * view in alertView.subviews){  
  60.         //如果类型是 UILabel  
  61.         if([view isKindOfClass:[UILabel class]]){  
  62.             //获取 UILabel 控件  
  63.             UILabel * label = (UILabel *) view;  
  64.             //设置 UILabel 控件右对齐  
  65.             label.textAlignment = UITextAlignmentRight;  
  66.         }  
  67.     }  
  68. }  
  69.   
  70. @end  



-- 界面展示效果 : 

 UIActionSheet 控件

1. UIActionSheet 简介

(1) UIActionSheet 作用

UIActionSheet 作用 : 该控件是显示在界面底部的按钮列表, 该控件 有 一个标题 和 多个按钮;

(2) UIActionSheet 按钮

UIActionSheet 固定按钮 : 

-- 取消按钮 : 灰色背景, 主要用于取消该 UIActionSheet 控件显示;

-- 销毁按钮 : 红色背景, 用于删除某记录时, 使用该按钮确认销毁;

(3) UIActionSheet 风格

UIActionSheet 支持风格 : 

-- UIActionSheetStyleDefault : 灰色背景上显示白色文字;

-- UIActionSheetStyleBlackTranselucent : 透明黑色背景上显示白色文字;

-- UIActionSheetBlackOpaque : 纯黑的背景上显示白色文字;

2. UIActionSheet 示例代码

UIActionSheet 示例代码 : 

-- 界面设计文件 : 

-- OCViewController.h : 

[objc] view plaincopy
 
  1. //  
  2. //  OCViewController.h  
  3. //  UIActionSheet  
  4. //  
  5. //  Created by octopus on 15-12-17.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10.   
  11. @interface OCViewController : UIViewController <UIActionSheetDelegate>  
  12. - (IBAction)click:(id)sender;  
  13.   
  14. @end  



-- OCViewController.m : 

[objc] view plaincopy
 
  1. //  
  2. //  OCViewController.m  
  3. //  UIActionSheet  
  4. //  
  5. //  Created by octopus on 15-12-17.  
  6. //  Copyright (c) 2015年 www.octopus.org.cn. All rights reserved.  
  7. //  
  8.   
  9. #import "OCViewController.h"  
  10.   
  11. @interface OCViewController ()  
  12.   
  13. @end  
  14.   
  15. @implementation OCViewController  
  16.   
  17. - (void)viewDidLoad  
  18. {  
  19.     [super viewDidLoad];  
  20.     // Do any additional setup after loading the view, typically from a nib.  
  21. }  
  22.   
  23. - (void)didReceiveMemoryWarning  
  24. {  
  25.     [super didReceiveMemoryWarning];  
  26.     // Dispose of any resources that can be recreated.  
  27. }  
  28.   
  29. - (IBAction)click:(id)sender {  
  30.     UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:@"是否删除" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles: @"按钮一", @"按钮二", nil nil];  
  31.     actionSheet.actionSheetStyle = UIActionSheetStyleAutomatic;  
  32.     [actionSheet showInView:self.view];  
  33. }  
  34.   
  35. - (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{  
  36.     UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat : @"点击了第 %d 个按钮", buttonIndex] delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil nil];  
  37.     [alertView show];  
  38. }  
  39. @end  



-- 运行界面展示 : 

原文地址:https://www.cnblogs.com/sungk/p/5108838.html