UIAlertController的使用-修改按钮颜色,添加输入框

如果选择的是UIAlertView,想要是使用block回调选择的按钮,可以使用框架STAlertView
https://github.com/LittleMoster/STAlertView

修改按钮的文字

 //弹出选择框,询问用户是否切换城市
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否切换城市?"message:messageStr preferredStyle:UIAlertControllerStyleAlert];
    
    
    //修改按钮的颜色
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"切换"style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
      //点击事件的处理
        
    }];
    [sure setValue:[UIColor orangeColor] forKey:@"_titleTextColor"];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
        
        
    }];
    [cancle setValue:[UIColor orangeColor] forKey:@"_titleTextColor"];
    [alert addAction:sure];
    [alert addAction:cancle];
    
    [self presentViewController:alert animated:true completion:nil];

带输入框的弹出框

#pragma  mark --弹出输入输入金额的框
-(void)UIalertViewShow
{
    NSString * messageStr=[NSString stringWithFormat:@"请输入支付服务的费用"];

   
    UIAlertController *alertCtl = [UIAlertController alertControllerWithTitle:@"金额" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alertCtl addTextFieldWithConfigurationHandler:^(UITextField *textField){
        textField.placeholder = messageStr;
        textField.keyboardType =UIKeyboardTypeNumbersAndPunctuation;
        [textField becomeFirstResponder];
    }];
    
       //修改按钮的颜色
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定"style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
       
        //读取收入框的内容的方法
        UITextField *textF = alertCtl.textFields.firstObject;
        NSLog(@"%@",textF.text);
   
        
    }];
    [sure setValue:MainColor forKey:@"_titleTextColor"];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnull action) {
  
        
    }];
    [cancle setValue:MainColor forKey:@"_titleTextColor"];
    [alertCtl addAction:sure];
    [alertCtl addAction:cancle];
    
    [self presentViewController:alertCtl animated:true completion:nil];
}

简单的用法

- (id)showAlertWithTitle:(NSString *)title {
    if (iOS8Later) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertController animated:YES completion:nil];
        return alertController;
    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alertView show];
        return alertView;
    }
}

- (void)hideAlertView:(id)alertView {
    if ([alertView isKindOfClass:[UIAlertController class]]) {
        UIAlertController *alertC = alertView;
        [alertC dismissViewControllerAnimated:YES completion:nil];
    } else if ([alertView isKindOfClass:[UIAlertView class]]) {
        UIAlertView *alertV = alertView;
        [alertV dismissWithClickedButtonIndex:0 animated:YES];
    }
    alertView = nil;
}

你可能感兴趣的:(UIAlertController的使用-修改按钮颜色,添加输入框)