UIAlertController 弹框提醒

传统的alertView

- (void)alertView
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你的操作时非法的,您要继续吗" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;
    [alert show];
}

传统的actionSheet

- (void)actionSheet
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"警告:你的操作时非法的,您要继续吗" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定"
                                              otherButtonTitles:@"关闭", nil];
    [sheet showInView:self.view];
 }

UIAlertController == UIAlertView + UIActionSheet

- (void)alertController
{
    // 危险操作:弹框提醒
    // 1.UIAlertView
    // 2.UIActionSheet
    // iOS8开始:UIAlertController == UIAlertView + UIActionSheet
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的操作时非法的,您要继续吗" preferredStyle:UIAlertControllerStyleAlert];

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

    [alert addAction:[UIAlertAction actionWithTitle:@"其它" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"点击了其它按钮");
    }]];

    // 添加文本框
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.textColor = [UIColor redColor];
        textField.text = @"123";
        [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
        //        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(usernameDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.secureTextEntry = YES;
        textField.text = @"123";
    }];

    [self presentViewController:alert animated:YES completion:nil];
}

- (void)usernameDidChange:(UITextField *)username
{
    NSLog(@"%@", username.text);
}

其他操作

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你的操作时非法的,您要继续吗" preferredStyle:UIAlertControllerStyleActionSheet];
    // 设置popover指向的item
    alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;

    // 添加按钮
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
        NSLog(@"点击了确定按钮");
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"点击了取消按钮");
    }]];

    [self presentViewController:alert animated:YES completion:nil];

}
// UIAlertControllerStyleActionSheet的使用注意
// 1.不能有文本框
// 2.在iPad中,必须使用popover的形式展示

// Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert
// 只能在UIAlertControllerStyleAlert样式的view上添加文本框

时间: 2024-08-20 14:18:23

UIAlertController 弹框提醒的相关文章

iOS开发 - UIAlertController 弹框提醒

传统的alertView - (void)alertView { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"你的操作时非法的,您要继续吗" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; alert.alertViewStyle = UIA

IOS UIAlertController 弹框 (ios 9.0 后代替了UIAlertView弹框 和 UIActionSheet下弹框)

在IOS 9.0 后 苹果官方宣布不再或不推荐使用UIAlertView 和 UIActionSheet 由UIAlertController进行代替两者 用控制器将两者合二为一 很简单 方便 下面就是关于UIAlertView的常用方法 #import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad {

IOS8 UIAlertController 弹框

本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35551769 IOS8中,Apple将UIActionSheet和UIAlertView整合成一个接口UIAlertController. 原来的是一个view,展示在window视图之上.现在改成了controller,展示方式变成由当前的controller直接present出来. 下面看看具体的接口: [html] view plaincopy UIAlertControll

C# 隔一段时间自动刷新弹框(医院病人超时提醒)

做一个简单的弹框提醒,这里需要用到线程,那什么是线程,我百度了一下,大概是这个意思: 通过System.Threading.Thread类可以开始新的线程,并在线程堆栈中运行静态或实例方法.可以通过Thread类的的构造方法传递一个无参数,并且不返回值(返回void)的委托(ThreadStart),这个委托的定义如下: [ComVisibleAttribute(true)] public delegate void ThreadStart() 我自己也不是特别懂,想了解的话建议直接百度. 今天

IOS 开发 弹框 UIAlertView 与UIAlertController

//View 里面的一个按钮 - (IBAction)showAlert:(id)sender { //ios8 之前 UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"你好,波多老师" message:@"听说您??有新戏要上映了" delegate:nil cancelButtonTitle:@"我来贡献票房来了" otherButtonTitles:nil, nil ]; [a

3种常用IOS弹框

目前为止,已经知道3种IOS弹框: 1.系统弹框-底部弹框 UIActionSheet (1)用法:处理用户非常危险的操作,比如注销系统等 (2)举例: UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定要注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButton

ios中的三种弹框

目前为止,已经知道3种IOS弹框: 1.系统弹框-底部弹框 UIActionSheet (1)用法:处理用户非常危险的操作,比如注销系统等 (2)举例: UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"确定要注销?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButton

UI弹框

1. ios7弹框 // ios7弹框的创建 // 弹框的创建 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"欢迎" message: @"欢迎来到德莱联盟" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil, nil]; // 显示弹框 [alert show]; 2.ios8弹框 // ios8弹框 //

IOS8 UIAlertController 提示框

IOS8中,Apple将UIActionSheet和UIAlertView整合成一个接口UIAlertController. 原来的是一个view,展示在window视图之上.现在改成了controller,展示方式变成由当前的controller直接present出来. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"This is Title" message:@"This