iOS中的UIAlertContrller

來源:互聯網
上載者:User

標籤:

#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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.