OC 提示框 UIAlertController UIAlertAction

1,299 阅读1分钟

必须要写在ViewController里面

{
    //创建并设置UIAlertController
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"恭喜中奖" message:@"您中了五百万美金" preferredStyle:UIAlertControllerStyleAlert];
    
     //创建文本框
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
    //文本框内输入提示
    textField.placeholder = @"请输入车名";
    //设置弹出键盘样式
    textField.keyboardType = UIKeyboardTypeDefault;
      }];
    
    //创建UIAlertAction
    UIAlertAction *conform = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //里面写点击这个按钮需要执行的内容
    self.rotateView.link.paused = NO;
    //拿到文本框
    UITextField *envirnmentNameTextField = alert.textFields.firstObject;
    //使用文本框内容
    car.name = envirnmentNameTextField.text;
    }];
    
    //添加UIAlertAction到UIAlertController
    [alert addAction:conform];
    
    //显示对话框
    [self presentViewController:alert animated:YES completion:nil];
}

ActionSheet:

image.png

- (void)logOut{
    //创建并设置UIAlertController
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"确定要注销吗?" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    //创建UIAlertAction
    //UIAlertActionStyleDefault:普通样式
    UIAlertAction *actionDefault = [UIAlertAction actionWithTitle:@"取消1" style:UIAlertActionStyleDefault handler:nil];
    
    //UIAlertActionStyleDestructive:文字会自动变成红色
    //Apply a style that indicates the action might change or delete data. 
    UIAlertAction *destructive = [UIAlertAction actionWithTitle:@"注销" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];  
    }];
    
    //UIAlertActionStyleCancel:会自动放在最下面,并割出来
    //Apply a style that indicates the action cancels the operation and leaves things unchanged.
    UIAlertAction *actionCancle = [UIAlertAction actionWithTitle:@"取消2" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

    }];
    
    [alertVc addAction:actionDefault];
    [alertVc addAction:destructive];
    [alertVc addAction:actionCancle];
    
    [self presentViewController:alertVc animated:YES completion:nil];

}