標籤:style io os 使用 sp cti on c log
iOS8中的UIAlertController包括了之前的UIAlertView和UIActionSheet,將它們集合成了自己的style;
1------------UIAlertControllerStyleAlert-----原有的UIAlertView
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"alertC" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
//preferredStyle--唯讀
//列印選擇的AlertController的類型 actionSheet--0 alertView--1
NSLog(@"%ld",alertC.preferredStyle);
//擷取alertC的標題和資訊
NSLog(@"%@",alertC.title);
NSLog(@"%@",alertC.message);
//更改標題
alertC.title = @"title change";
//直接給alertC添加事件執行按鈕actionTitle(只能添加一個)
/*
[alertC addAction:[UIAlertAction actionWithTitle:@"actionTitle" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//點擊actionTitle時的回調方法
}]];
*/
//添加多個按鈕
//沒有任何代理的情況下,在我們點擊alertView介面中的按鈕時,就不需要用buttonIndex來區分我們點擊的是哪個按鈕,同時一個介面上多個alertView時,也不需要用tag值來區分你到底點擊的是哪一個alertView上的按鈕了
//這樣便於我們的邏輯思維,但是代碼量並沒有減少
UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancle", @"Cancle action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
//cancle對應執行的代碼
NSLog(@"cancle action");
}];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"sure", @"Sure action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
//sure對應執行的代碼
NSLog(@"sure action");
}];
[alertC addAction:action];
[alertC addAction:action1];
//alertView的輸入框 sheet類型中沒有 但是sheet類型的UIAlertController可以添加textField 當使用者要顯示sheet時程式會崩
[alertC addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"textFiled";
}];
//在這裡不管是alertView還是actionSheet都是一個獨立的Cotroller,所以這裡需要通過presentViewController來展現我們的alertView或者actionSheet
[self presentViewController:alertC animated:YES completion:nil];
2---------------UIAlertControllerStyleActionSheet----原有的UIActionSheet
UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"alertC" message:@"message" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *action = [UIAlertAction actionWithTitle:NSLocalizedString(@"cancle", @"Cancle action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action) {
NSLog(@"cancle action");
}];
UIAlertAction *action1 = [UIAlertAction actionWithTitle:NSLocalizedString(@"sure", @"Sure action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(@"sure action");
}];
[alertC addAction:action];
[alertC addAction:action1];
[self presentViewController:alertC animated:YES completion:nil];
iOS8中UIAlertController的使用