UIAlertController的使用-修改按鈕顏色,添加輸入框

如果選擇的是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) {
      //點擊事件的處理
        
    }];
    [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ù)的費用"];

   
    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;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容