UIAlertView& UIActionSheet&UIAlertController

UIAlertView

在ios8之后deprecated

- (void)setUpAlertView
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"AlertView" message:@"哈哈哈哈哈哈哈啊哈哈哈" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"查看", nil];
    //文本输入
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
   //样式类型
/* 
    typedef NS_ENUM(NSInteger, UIAlertViewStyle) {
        UIAlertViewStyleDefault = 0,
        UIAlertViewStyleSecureTextInput,
        UIAlertViewStylePlainTextInput,
        UIAlertViewStyleLoginAndPasswordInput
    }
 */
    [alertView show];
}
#pragma mark - UIActionSheetDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld" ,buttonIndex);
}
UIAlertView& UIActionSheet&UIAlertController_第1张图片
alertview1.gif

UIActionSheet

在ios8之后deprecated

- (void)setUpActionSheet
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"查看评论",@"回复", nil];
    [actionSheet addButtonWithTitle:@"删除"];
    [actionSheet showInView:self.view];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"%ld" ,buttonIndex);
}
UIAlertView& UIActionSheet&UIAlertController_第2张图片
alertsheet1.gif

UIAlertController

阅读UIActionSheet
和UIAlertView
类文档,会发现它们有很多方法相似,UIAlertController就是结合这两者的共同体,可以用UIAlertController设置preferredStyle来创建alertView和alertSheet.

- (void)setUpAlertControllerWithSheet
{
    _alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *showAllInfoAction = [UIAlertAction actionWithTitle:@"查看" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"查看...");
    }];
    UIAlertAction *commentAction = [UIAlertAction actionWithTitle:@"评论" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"评论...");
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消...");
    }];
    
    [_alertController addAction:cancelAction];
    [_alertController addAction:commentAction];
    [_alertController addAction:showAllInfoAction];
    
    [self presentViewController:_alertController animated:YES completion:nil];
}
UIAlertView& UIActionSheet&UIAlertController_第3张图片
alertSheet2.gif
- (void)setUpAlertControllerWithAlertView
{
    _alertController = [UIAlertController alertControllerWithTitle:@"登 录" message:@"hi hello!" preferredStyle:UIAlertControllerStyleAlert];
    
    //文本输入
    [_alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"学号";
    }];
    
   //密码输入
    [_alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
    }];
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (_alertController.textFields.count >= 2)
        {
            UITextField *account_textfield = _alertController.textFields.firstObject;
            UITextField *password_textfield = [_alertController.textFields objectAtIndex:1];
            NSLog(@"学号:%@,密码:%@",account_textfield.text,password_textfield.text);
        }
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"cancel...");
    }];
    [_alertController addAction:confirmAction];
    [_alertController addAction:cancelAction];
    
    [self presentViewController:_alertController animated:YES completion:nil];
}
UIAlertView& UIActionSheet&UIAlertController_第4张图片
alertView2.gif

你可能感兴趣的:(UIAlertView& UIActionSheet&UIAlertController)