zoukankan      html  css  js  c++  java
  • iOS开发——UIAlertController

      iOS8之后,UIAlertView和UIActionSheet被干掉了,取而代之的是UIAlertController和UIAlertAction。

      UIAlertController有两种样式,一个是UIAlertControllerStyleAlert,就是以前的UIAlertView,还有一个就是UIAlertControllerStyleActionSheet,也就是以前的UIActionSheet。UIAlertControllerStyleAlert模式中可以加输入框,UIAlertControllerStyleActionSheet不行。为了更像点样子,我设置文本框有输入才能点击确定。

      废话不多说,上代码:

    //

    //  ViewController.m

    //  Demo-hehehe

    //

    //  Created by yyt on 16/4/21.

    //  Copyright © 2016年 yyt. All rights reserved.

    //

    #import "ViewController.h"

    @interface ViewController ()<UIAlertViewDelegate>

    @property(nonatomic,strong) UITextField *myTextField;

    @property(nonatomic,strong) UIAlertAction *otherAction;

    @end

    @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

        

        UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeSystem];

        alertButton.frame = CGRectMake(40, 140, 200, 40);

        alertButton.backgroundColor = [UIColor orangeColor];

        [alertButton setTitle:@"AlertButton" forState:UIControlStateNormal];

        [alertButton addTarget:self action:@selector(clickAlertButton:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:alertButton];

        

        UIButton *actionSheetButton = [UIButton buttonWithType:UIButtonTypeSystem];

        actionSheetButton.frame = CGRectMake(40, 200, 200, 40);

        actionSheetButton.backgroundColor = [UIColor orangeColor];

        [actionSheetButton setTitle:@"ActionSheetButton" forState:UIControlStateNormal];

        [actionSheetButton addTarget:self action:@selector(clickActionSheetButton:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:actionSheetButton];

    }

    - (void)clickAlertButton:(UIButton*)sender {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"AlertButton" message:nil preferredStyle:UIAlertControllerStyleAlert];

        

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

            NSLog(@"11111");

        }];

        [alertController addAction:cancelAction];

        

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

            NSLog(@"222222");

        }];

        self.otherAction = otherAction;

        otherAction.enabled = NO;

        [alertController addAction:otherAction];

        

        [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {

            self.myTextField = textField;

            

            textField.placeholder = @"必填";

            

            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextFieldTextDidChangeNotification:) name:UITextFieldTextDidChangeNotification object:textField];

        }];

        

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

        

    }

    - (void)handleTextFieldTextDidChangeNotification:(NSNotification *)notification {

        if(self.myTextField.text.length > 0) {

            self.otherAction.enabled = YES;

        } else {

            self.otherAction.enabled = NO;

        }

    }

    - (void)clickActionSheetButton:(UIButton*)sender {

        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"ActionSheet" message:nil preferredStyle:UIAlertControllerStyleActionSheet];

        

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

            NSLog(@"11111");

        }];

        [alertController addAction:cancelAction];

        

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

            NSLog(@"222222");

        }];

        [alertController addAction:otherAction];

        

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

    }

    @end

  • 相关阅读:
    【后端】Python学习笔记
    【学习】JennyHui学英语
    【学习】JennyHui学英语
    【英语】Bingo口语笔记(3)
    LoadRunner目录分析
    性能测试常见用语
    [转]黑盒测试用例设计方法
    RUP
    软件质量管理杂谈
    关于BUG
  • 原文地址:https://www.cnblogs.com/yyt-hehe-yyt/p/5422386.html
Copyright © 2011-2022 走看看