UIAlertController简单使用

简介: UIAlertController简单使用

上一篇博客中有用到UIAlertView和UIActionSheet,鉴于在iOS8中被废弃,所以简单说明下心提供的UIAlertController的用法,不过也不用急着换,还是可以正常使用的。

1.png

博主只说明三个简单的用法,


第一:UIAlertView

直接看代码:

- (void)AlertView
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"这是一个UIAlertController,UIAlertView已经废弃了." preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
        //取消操作
    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        //确定操作
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:okAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

第二:UIActionSheet

- (void)ActionSheet
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"警告" message:@"这是一个UIAlertController,UIActionSheet已经废弃了." preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
        //取消操作
    }];
    //UIAlertActionStyleDestructive有着重的意思,使用这个常量标题变为红色
    UIAlertAction *firstAction = [UIAlertAction actionWithTitle:@"第一个" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
        //确定操作
    }];
    UIAlertAction *secondAction = [UIAlertAction actionWithTitle:@"第二个" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        //确定操作
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:firstAction];
    [alertVC addAction:secondAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

第三:带输入框的UIAlertView

- (void)AlertTextField
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"登陆" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
        //取消操作
    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action){
        //确定操作
        //也可在此判断输入位数是否符合要求
        NSLog(@"%@",loginTextField.text);
        NSLog(@"%@",passTextField.text);
    }];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = @"登录";
    }];
    [alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
    }];
    loginTextField = alertVC.textFields.firstObject;
    passTextField = alertVC.textFields.lastObject;
    [alertVC addAction:cancelAction];
    [alertVC addAction:okAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}

用法也不难,看一下就好,博主写的只是基础用法,还有其他一些用法,不是很常用,所以就不再说明了,用到的话就自己查查吧,除了这么写外,还可以自定义,博主前面曾经封装过一个自定义的UIAlertView,想看的去“控件封装”分类中查看。


网上也有更详细的介绍,博主这也是班门弄斧了。Demo下载地址就不放上去了,上面的方法加一个触发方式就好了。

目录
相关文章
|
1天前
|
JavaScript 前端开发 Swift
swift-UISegmentedControl和UIWebView的用法
这是关于 `UISegmentedControl`和 `UIWebView`的基本用法。它们是iOS应用中常用的界面元素,可以帮助您实现用户友好的交互和显示Web内容。详细的用法可以参考苹果官方文档以及在线教程和示例代码。
14 3
|
10月前
|
Dart
Flutter EasyRefreshList使用方法 下拉加载 上拉刷新
Flutter EasyRefreshList使用方法 下拉加载 上拉刷新
|
Swift
Swift - UIAlertController的简单使用
Swift - UIAlertController的简单使用
117 0
Swift - UIAlertController的简单使用
|
iOS开发 索引
IOS UIAlertView(警告框)方法总结
IOS UIAlertView(警告框)方法总结
139 0
IOS UIAlertView(警告框)方法总结
|
Android开发 数据格式 XML
FloatingActionButton的简单使用
FloatingActionButton是Support Design Library库中引入的一个新的控件,外观时尚新颖,受到很多开发者的好评。 如何使用FloatingActionButton 使用FloatingActionButton其实非常简单只需要在布局文件中引入控件即可,不过它的属性有点多,我们先来介绍一下它的属性。
1789 0