標籤:
#import "RootViewController.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; //************************只是為了建立一個點擊事件,用來觸發能用UIAlertController處理的事件***************** //建立一個分段控制項 UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:@[@"放棄",@"菜單"]]; segment.tintColor = [UIColor orangeColor]; segment.frame = CGRectMake(60, 100, 200, 40); [segment addTarget:self action:@selector(handle:) forControlEvents:UIControlEventValueChanged]; [self.view addSubview:segment]; [segment release]; }//*******************處理點擊事件 (用 UIAlertControl (ios 8 之後才能用???? ) 處理)-------所講重點;/** * 下面的是我所分享的這個空間,別看代碼長,其實 case0 和 case1 是 裡面幾乎一樣的代碼,一個是為了處理警告框(UIAertView),另一個是處理動作表(UIActionSheet).其實 只是對UIAlertControl的初始化換了個類型而已,大家不要怕代碼長; 都是重複的,功能挺強大的;大家認真看看???????? * *(重點是 裡面的Block 塊,這裡我只是打了個NSLog,大家下去自己多總結????????) */- (void)handle:(UISegmentedControl *)sender{ switch (sender.selectedSegmentIndex) { // 下面實現 用 UIAlertController處理 UIAlertView的功能: case 0:{ //1 .建立對象 UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"確定要退出麼?" preferredStyle: UIAlertControllerStyleAlert]; //2. 建立 觸發項 //建立觸發項時注意它的類型(Style) 是三個枚舉值,有不同的作用!! UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^void(UIAlertAction *action){NSLog(@"什麼都不操作????");}]; //建立 第二個觸發項 (還可以繼續建立,這裡只用兩個) UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style: UIAlertActionStyleDestructive handler:^void(UIAlertAction *action){NSLog(@"您執行了您的操作??"); }]; //3.添加觸發項 (把剛才建立的觸發項添加到這個UIAlertController的對象(alertVC)上)???? [alertVC addAction:cancelAction]; [alertVC addAction:okAction]; //4.添加 輸入框 實現使用者賬戶的輸入 [alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"使用者名稱"; }]; ////添加 輸入框 實現使用者密碼的輸入 [alertVC addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"密碼"; textField.secureTextEntry = YES; }]; //5.模態 alertVC 控制器 到模態視窗進行顯示???????? [self presentViewController:alertVC animated:YES completion:nil]; } break; // 下面實現 用 UIAlertController處理 UIActionSheet的功能 case 1: { //1 .建立對象 UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"確定要退出麼?" preferredStyle: UIAlertControllerStyleActionSheet]; //2. 建立 觸發項 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^void(UIAlertAction *action){NSLog(@"什麼都不操作????");}]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"確定" style: UIAlertActionStyleDestructive handler:^void(UIAlertAction *action){NSLog(@"您執行了您的操作??"); }]; //3.添加觸發項 [alertVC addAction:cancelAction]; [alertVC addAction:okAction]; //4.模態 alertVC 控制器 到模態視窗進行顯示???????? [self presentViewController:alertVC animated:YES completion:nil]; // 注意: // 這個只有 四 步 哦! 比UiAlertView少了個 添加 輸入框的功能,因為貌似 動作表沒有添加輸入框的功能!????????! } break; default: break; } }//到這裡 UIAlertController 的一個初始化方法 和 一個添加操作項的方法 以及一個為 警告框 添加文字框的方法都在上面的代碼裡了!這個控制器內容雖然不多,但可以實現的功能是很強大的,因為裡面涉及了Block 塊!所以 還是請大家自己去好好看看為好!這裡就不多說了!????// 另外 UIAlertController 裡面還有五個小的屬性,其中三個是唯讀,大家可以去標頭檔 或者 API裡查看,這裡就不介紹了! 大家再見啊!作??????????@end
iOS中的UIAlertContrller