UIAlertController UIAlertAction (UIAlertView, UIActionSheet)

- (void)viewDidLoad {

    [super viewDidLoad];

    NSArray * items = @[@"课程",@"机构"];

    jobAndCompanySegment = [[UISegmentedControl alloc] initWithItems:items];

    jobAndCompanySegment.frame = CGRectMake(320/4,100, 480/2, 40);

    jobAndCompanySegment.tintColor = [UIColor orangeColor];

    [jobAndCompanySegment addTarget:self action:@selector(segChanged:) forControlEvents:UIControlEventValueChanged];

    jobAndCompanySegment.selectedSegmentIndex = 0;

    [self.view addSubview:jobAndCompanySegment];

}


- (void)segChanged:(UISegmentedControl *)sender

{

    

    // 1. 创建UIAlertControl变量,但并不穿GIAn

    UIAlertController *alertController = nil;

    

    

    // 2. 根据点击的item创建不同样式的alertController

    switch (sender.selectedSegmentIndex) {

        case 0: { // 弹出AlertView

            alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleAlert];

            break;

        }

        case 1: { // 弹出ActionSheet

            alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message" preferredStyle:UIAlertControllerStyleActionSheet];

            break;

        }

        default:

            break;

    }

    

    

    // 3. 添加取消按钮

    // 3.1 UIAlertAction 表示一个按钮,同时,这个按钮带有处理事件的block

    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {

        NSLog(@"取消");

    }];

    // 3.2 添加到alertController

    [alertController addAction:action];

    

    

    // 4. 添加需要谨慎操作的按钮,文字默认是红色的

    [alertController addAction:({

        UIAlertAction *action = [UIAlertAction actionWithTitle:@"谨慎操作的按钮" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

            NSLog(@"谨慎操作的按钮");

        }];

        action;

    })];

    

    

    // 5. 添加确定按钮

    [alertController addAction:({

        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

            NSLog(@"确定");

            

            // 打印输入框的用户名和密码

            NSString *userNameStr = [alertController.textFields[0] text];

            NSString *passwordStr = [alertController.textFields[1] text];

            NSLog(@"userName is: %@  password is: %@", userNameStr, passwordStr);

        }];

        action;

    })];

    

    

    // 6. 添加输入框到alertView中,注意,actionSheet是没有办法添加textField的,强行添加会Crash

    if (alertController.preferredStyle == UIAlertControllerStyleAlert) {

        // 添加用户名输入框

        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

            // 给输入框设置一些信息

            textField.placeholder = @"请输入用户名";

            textField.textAlignment = NSTextAlignmentCenter;

        }];

        // 添加密码输入框

        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

            textField.placeholder = @"请输入密码";

            textField.secureTextEntry = YES;

            textField.textAlignment = NSTextAlignmentCenter;

        }];

    }

    

    

    // 7. 显示(使用模态视图推出)

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

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值