iOS开发 - 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-02 08:01:15

iOS开发 - UIAlertController 弹框提醒的相关文章

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开发-自定义AlterView(iOS 7)

App中不可能少了弹框,弹框是交互的必要形式,使用起来也非常简单,不过最近需要自定义一个弹框,虽然iOS本身的弹框已经能满足大部分的需求,但是不可避免还是需要做一些自定义的工作.iOS7之前是可以自定义AlterView的,就是继承一下UIAlterView,然后初始化的时候通过addSubview添加自定义的View,但是iOS7之后这样做就不行了,不过还好有开源项目可以解决这个问题. iOS默认弹框 viewDidLoad中添加两个按钮,代码如下: UIButton *orignalBtn=

IOS 开发 弹框 UIAlertView 与UIAlertController

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

iOS开发- 自动消失的弹出框

- (void)timerFireMethod:(NSTimer*)theTimer//弹出框 { UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo]; [promptAlert dismissWithClickedButtonIndex:0 animated:NO]; promptAlert =NULL; } - (void)showAlert:(NSString *) _message{//时间 UIAlertView *

自学iOS开发小功能之三:弹框的两种方式(iOS8.3之后新的方式,之前的已经弃用)

1.弹框出现在屏幕中间位置 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否退出" preferredStyle: UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActio

iOS开发——ActionSheet的使用与弹出选择对话框

在我们的iOS开发中,常会见到如下界面的需求: . 也就是点击按钮,出现选择提示框,我们今天使用两种方式(ActionSheet和AlertController)来实现该功能.示例代码上传至: https://github.com/chenyufeng1991/iOS-ActionSheet   . [使用ActionSheet实现] (1)实现代码如下: #import "ViewController.h" @interface ViewController ()<UIActi