您的位置:首页 > 产品设计 > UI/UE

iOS 8新特性—— UIAlertController

2015-11-28 19:37 471 查看
//
//  ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//危险操作:弹框提醒
//1.UIAlertView
//    [self alertView];
//2.UIActionSheet
//    [self actionSheet];

//iOS 8开始:UIAlertController == UIAlertView + UIAcitonSheet
//  UIAlertControllerStyleActionSheet = 0,
//  UIAlertControllerStyleAlert
UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"警告" message:@"你有严重的精神病" preferredStyle:UIAlertControllerStyleAlert];

//添加文本框
[ac addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.textColor = [UIColor redColor];
textField.text = @"123";
//监听textField文字的改变
[textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
}];
[ac addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.secureTextEntry = YES;
textField.text = @"password";
}];

//添加按钮
__weak typeof (ac) weakAc = ac;
[ac addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了确定按钮--%@--%@", [weakAc.textFields.firstObject text], [weakAc.textFields.lastObject text]);
}]];
[ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消按钮");
}]];

//弹出
[self presentViewController:ac animated:YES completion:nil];

}

/**
*  监听textField文字的改变
*/
- (void)usernameDidChange:(UITextField *)usename
{
NSLog(@"usename=%@",usename.text);
}

- (void)actionSheet
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"警告" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"关闭", nil];
[sheet showInView:self.view];
}

- (void)alertView
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"警告" message:@"你有严重的精神病" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//加一个密码文本框
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
[alert show];
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: