iOS 弹窗UIAlertView、UIActionSheet、UIAlertController简述 —— HERO博客
2016-02-22 22:28
573 查看
UIAlertView:
在屏幕中间的弹窗,可以设置标题、详细信息、按钮文字及个数。
如图27-1、27-2,只设置了一个取消按钮和多添加一个确认按钮的效果。仍然可以继续添加按钮,按钮会纵向向下排列。需要通过代理设置其他按钮点击事件,否则点击仍是取消效果。Title、Message为nil时不显示。下面贴上相关代码:
UIActionSheet:
在屏幕底部的弹窗,可以设置标题、按钮文字及个数。
如图27-3,一个UIActionSheet弹窗效果。可以添加多个按钮,需要通过代理设置其他按钮点击事件,否则点击仍是取消效果。如图27-4,Title、cancelButtonTitle、destructiveButtonTitle、otherButtonTitles为nil时不显示。下面贴上相关代码:
UIAlertController:
iOS8推出的新controller,替代了UIAlertView和UIActionSheet,但UIAlertView和UIActionSheet仍可以使用。UIAlertController可以添加输入框,操作更加简便、灵活。
如图27-5,一个UIAlertControllerStyleAlert类型弹窗效果。可以添加多个按钮,设置按钮点击后block事件。如图27-6,一个UIAlertControllerStyleActionSheet类型弹窗效果。下面贴上相关代码:
在屏幕中间的弹窗,可以设置标题、详细信息、按钮文字及个数。
如图27-1、27-2,只设置了一个取消按钮和多添加一个确认按钮的效果。仍然可以继续添加按钮,按钮会纵向向下排列。需要通过代理设置其他按钮点击事件,否则点击仍是取消效果。Title、Message为nil时不显示。下面贴上相关代码:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end #import "ViewController.h" @interface ViewController ()<UIAlertViewDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; //创建控件 [self creatContorl]; } - (void)creatContorl { UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 150, 100, 30)]; btn.backgroundColor = [UIColor orangeColor]; [btn setTitle:@"测试按钮" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnOnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnOnClick { //初始化 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"这是Title" message:@"这是Message" delegate:self cancelButtonTitle:@"取消按钮" otherButtonTitles:@"其他按钮1", @"其他按钮2", @"其他按钮3", nil]; //显示alertView [alert show]; } #pragma mark - UIAlertViewDelegate //根据被点击按钮的索引处理点击事件 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"clickButtonAtIndex:%ld", buttonIndex); } //AlertView取消按钮点击事件 -(void)alertViewCancel:(UIAlertView *)alertView { NSLog(@"alertViewCancel"); } //AlertView即将显示时调用 -(void)willPresentAlertView:(UIAlertView *)alertView { NSLog(@"willPresentAlertView"); } //AlertView已经显示时调用 -(void)didPresentAlertView:(UIAlertView *)alertView { NSLog(@"didPresentAlertView"); } //ALertView即将消失时调用 -(void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"willDismissWithButtonIndex"); } //AlertView已经消失时调用 -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"didDismissWithButtonIndex"); } @end
UIActionSheet:
在屏幕底部的弹窗,可以设置标题、按钮文字及个数。
如图27-3,一个UIActionSheet弹窗效果。可以添加多个按钮,需要通过代理设置其他按钮点击事件,否则点击仍是取消效果。如图27-4,Title、cancelButtonTitle、destructiveButtonTitle、otherButtonTitles为nil时不显示。下面贴上相关代码:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end #import "ViewController.h" @interface ViewController ()<UIActionSheetDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; //创建控件 [self creatContorl]; } - (void)creatContorl { UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 250, 100, 30)]; btn.backgroundColor = [UIColor orangeColor]; [btn setTitle:@"测试按钮" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnOnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnOnClick { //初始化 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"这是title" delegate:self cancelButtonTitle:@"取消按钮" destructiveButtonTitle:@"确定按钮" otherButtonTitles:@"其他按钮1", @"其他按钮2", nil]; //在self.view中显示弹窗 [actionSheet showInView:self.view]; } #pragma mark - UIActionSheetDelegate //根据被点击按钮的索引处理点击事件 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"clickButtonAtIndex:%ld", buttonIndex); } //弹窗视图即将显示时调用 - (void)willPresentActionSheet:(UIActionSheet *)actionSheet { NSLog(@"willPresentActionSheet"); } //弹窗视图已经显示时调用 - (void)didPresentActionSheet:(UIActionSheet *)actionSheet { NSLog(@"didPresentActionSheet"); } //弹窗视图即将消失时调用 - (void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"willDismissWithButtonIndex"); } //弹窗视图已经消失时调用 - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex { NSLog(@"didDismissWithButtonIndex"); } @end
UIAlertController:
iOS8推出的新controller,替代了UIAlertView和UIActionSheet,但UIAlertView和UIActionSheet仍可以使用。UIAlertController可以添加输入框,操作更加简便、灵活。
如图27-5,一个UIAlertControllerStyleAlert类型弹窗效果。可以添加多个按钮,设置按钮点击后block事件。如图27-6,一个UIAlertControllerStyleActionSheet类型弹窗效果。下面贴上相关代码:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; //创建控件 [self creatContorl]; } - (void)creatContorl { UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 30)]; btn.backgroundColor = [UIColor orangeColor]; [btn setTitle:@"测试按钮" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(btnOnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; } - (void)btnOnClick { //初始化 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"这是title" message:@"这是message" preferredStyle:UIAlertControllerStyleAlert]; //创建按钮 UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确认按钮" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"确认按钮block"); //取出输入框文字 UITextField *login = alertController.textFields.firstObject; UITextField *password = alertController.textFields.lastObject; NSLog(@"loginText:%@, passwordText:%@", login.text, password.text); }]; //取消按钮(只能创建一个) UIAlertAction *cancelAction = b98c [UIAlertAction actionWithTitle:@"取消按钮" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"取消按钮block"); }]; //警告按钮 UIAlertAction *structAction = [UIAlertAction actionWithTitle:@"警告按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSLog(@"警告按钮block"); }]; //将按钮添加到UIAlertController对象上 [alertController addAction:sureAction]; [alertController addAction:cancelAction]; [alertController addAction:structAction]; //添加文本框(只能在UIAlertController的UIAlertControllerStyleAlert样式下添加) [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"请输入账号"; textField.secureTextEntry = YES; }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"请输入密码"; textField.secureTextEntry = YES; }]; //显示弹窗视图控制器 [self presentViewController:alertController animated:YES completion:nil]; } @end
相关文章推荐
- 在 Swift 中测试 UIAlertController的方法
- iphone:自定义UIAlertView
- 在iOS 8中使用UIAlertController
- [Swift]UIKit学习之警告框:UIAlertController和UIAlertView
- 兼容iOs7的自定义alertView
- iOS8新特性 UIPresentationController(一)
- iOS 8 UIAlertController 控件
- UIAlertView的基本使用和对话框中按钮的事件处理方法
- UIButton,UIAlertView
- UIAlertController中UIAlertControllerStyleActionSheet与PickerView选择器的结合使用
- UIAlertView&UIActionSheet
- UIAlertView、UIActionSheet
- UIAlertView is deprecated in iOS 8.
- UIAlertController replaces the UIActionSheet and UIAlertView
- iOS9 UIAlertView的简单使用
- iOS最好用的弹出框
- (原创)iOS tableview系统左滑删除 左滑置顶 自定义左滑事件功能 输入框弹出
- 获取UIAlertController中textField的值
- 在iOS 8中使用UIAlertController
- 解析UITableViewCell的重用