如果選擇的是UIAlertView,想要是使用block回調(diào)選擇的按鈕擒悬,可以使用框架STAlertView
https://github.com/LittleMoster/STAlertView
修改按鈕的文字
//彈出選擇框模她,詢問用戶是否切換城市
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"是否切換城市?"message:messageStr preferredStyle:UIAlertControllerStyleAlert];
//修改按鈕的顏色
UIAlertAction *sure = [UIAlertAction actionWithTitle:@"切換"style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
//點(diǎn)擊事件的處理
}];
[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:@"請輸入支付服務(wù)的費(fèi)用"];
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) {
//讀取收入框的內(nèi)容的方法
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;
}