UIAlertController

1,最简单的显示:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"meassge" preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];

UIAlertControllerStyleActionSheet(最下面弹出)

2,添加按钮以及处理的方法:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Title" message:@"meassge" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *Okaction = [UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:nil];
//若要警告用户更改数据,按钮字体变红色选用UIalertActionStyleDestructive;
UIAlertAction *cancelActio = [UIAlertAction actionWithTitle:@"cancel" style:UIAlertActionStyleCancel handler:nil];
//处理的事情放到block里面
    [alert addAction:Okaction];
    [alert addAction:cancelActio];
    [self presentViewController:alert animated:YES completion:nil];

3,输入数据:

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录" message:@"密码" preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.text = @"登录";
    }];
    [self presentViewController:alert animated:YES completion:nil];

 

4,假定我们要让“登录”文本框中至少有3个字符才能激活“好的”按钮。很遗憾的是,在UIAlertController中并没有相应的委托方法,因此我们需要向“登录”文本框中添加一个Observer。Observer模式定义对象间的一对多的依赖关系,当一个对象的状态发生改变时, 所有依赖于它的对象都得到通知并被自动更新。我们可以在构造代码块中添加如下的代码片段来实现。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登录用户名" message:@"密码" preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"登录名至少三位";
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didTextfieldChanger:) name:@"UITextFieldTextDidChangeNotification" object:textField];
        
    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [[NSNotificationCenter defaultCenter]removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:nil];//注意通知名称要唯一,
        
    }];
    okAction.enabled = NO;
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)didTextfieldChanger:(NSNotification *)notifi{
    UIAlertController *alert = (UIAlertController *)self.presentedViewController;
    if (alert) {
        NSLog(@"did");
        UITextField *login = alert.textFields.firstObject;
        UIAlertAction *act = alert.actions.lastObject;
        act.enabled = login.text.length>2;
    }
}

 

4,其他

当需要给用户展示一系列选择的时候,上拉菜单就能够派上大用场了(sheet)。和对话框不同,上拉菜单的展示形式和设备大小有关。在iPhone上(紧缩宽度),上拉菜单从屏幕底部升起。在iPad上(常规宽度),上拉菜单以弹出框的形式展现。

不能在上拉菜单中添加文本框,如果您强行作死添加了文本框,那么就会荣幸地得到一个运行时异常:

* Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert’

原文地址:https://www.cnblogs.com/yangqinglong/p/5570173.html