一。UIAlertControllerStyleActionSheet底部 數組
typedef NS_ENUM(NSInteger, UIAlertControllerStyle) { app
UIAlertControllerStyleActionSheet = 0,底部 spa
UIAlertControllerStyleAlert 中間 code
} server
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"自定義背景" message:nil preferredStyle:UIAlertControllerStyleActionSheet];//建立對象 [self presentViewController:alertController animated:YES completion:nil];//展現alertController,相似於拍照的UIImagePickerController,是一個試圖控制器。 UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"cancel");//點擊按鈕觸發的事件在這裏寫 }];//建立按鈕 UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"默認default" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"默認default"); }]; UIAlertAction *defaultAction1 = [UIAlertAction actionWithTitle:@"默認default1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { NSLog(@"默認default1"); }]; UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置reset" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSLog(@"reset"); }]; UIAlertAction *resetAction1 = [UIAlertAction actionWithTitle:@"重置reset1" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSLog(@"reset1"); }]; [alertController addAction:cancleAction];//添加按鈕 [alertController addAction:defaultAction1]; [alertController addAction:defaultAction]; [alertController addAction:resetAction]; [alertController addAction:resetAction1];
注:在蘋果上,Cancel按鈕在左邊/最底部,其餘的按添加順序顯示。 對象
UIAlertActionStyle有三種:UIAlertActionStyleDefault = 0, UIAlertActionStyleCancel,和UIAlertActionStyleDestructive,除了style爲cancel只能夠addiction一次,其餘的能夠添加多個按鈕。 three
二。添加文本輸入框:UIAlertControllerStyleAlert,中間 事件
一、文本輸入框只能添加到Alert的風格中,ActionSheet是不容許的; get
二、UIAlertController具備只讀屬性的textFields數組,須要可直接按本身須要的順序添加; it
三、添加方式是使用block,參數是UITextField;
四、添加UITextField監聽方法和實現方法。
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"自定義背景" message:nil preferredStyle:UIAlertControllerStyleAlert];//建立對象 UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"cancel");//點擊按鈕觸發的事件在這裏寫 }];//建立按鈕 [alertController addAction:cancleAction];//添加上去 [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"111111"; }];//添加輸入框 [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"34444"; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldDidChanged:) name:UITextFieldTextDidChangeNotification object:textField];//添加UITextFieldTextDidChangeNotification通知,一旦輸入框text改變,在textfieldDidChanged:方法裏可檢測到輸入狀況 }]; [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.placeholder = @"jianting"; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfieldDidChanged:) name:UITextFieldTextDidChangeNotification object:textField]; }]; UIAlertAction *getAction = [UIAlertAction actionWithTitle:@"默認default" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { UITextField *login = alertController.textFields[0]; UITextField *password = alertController.textFields[1]; UITextField *three = alertController.textFields[2]; [self.view endEditing:YES]; NSLog(@"登錄:%@, 密碼:%@,three : %@", login.text, password.text,three.text ); }];//獲取輸入框中的內容 [alertController addAction:getAction];//添加按鈕,點擊時觸發上面handle的操做。 [self presentViewController:alertController animated:YES completion:nil];//展現視圖控制器,這個必須寫在後面,寫在添加textfield以前顯示不出輸入框的。
- (void)textfieldDidChanged:(NSNotification *)noti {
NSLog(@"\n\n\n%s, noti = %@", __func__, noti);
}
這個是監聽方法示例。三。一個自定義標題顏色文字的實現。
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"Dont care what goes here, since we're about to change below" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@"Presenting the great... Hulk Hogan!"]; [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50.0] range:NSMakeRange(24, 11)]; [alertVC setValue:hogan forKey:@"attributedTitle"]; UIAlertAction *button = [UIAlertAction actionWithTitle:@"Label text" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){ //add code to make something happen once tapped }]; // UIImage *accessoryImage = [UIImage imageNamed:@"someImage"]; // [button setValue:accessoryImage forKey:@"image"]; [alertVC addAction:button]; [self presentViewController:alertVC animated:YES completion:nil];