[iOS學(xué)習(xí)]警告框和操作表

警告框

iOS中警告框給用戶警告或提示,最多兩個(gè)按鈕,超過(guò)兩個(gè)就應(yīng)該使用操作表。由于在iOS中警告框是模態(tài)的,因此不應(yīng)該隨意使用。
警告框在iOS8之前使用UIAlertView視圖,在iOS8之后推出UIAlertController控制器,可是實(shí)現(xiàn)警告框和操作表。UIAlertController控制器中不僅可以添加按鈕,還可以添加文本框和自定義視圖到警告框和操作表,響應(yīng)事件不用委托協(xié)議實(shí)現(xiàn)。
下面是如何通過(guò)代碼實(shí)現(xiàn)添加警告框:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    CGRect screen = [UIScreen mainScreen].bounds;
    
    UIButton *buttonAlertView = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonAlertView setTitle:@"Test警告框" forState:UIControlStateNormal];
    CGFloat buttonAlertViewWidth = 100;
    CGFloat buttonAlertViewHeight = 30;
    CGFloat buttonAlertViewTopView = 130;
    
    buttonAlertView.frame = CGRectMake((screen.size.width - buttonAlertViewWidth)/2, buttonAlertViewTopView, buttonAlertViewWidth, buttonAlertViewHeight);
    [buttonAlertView addTarget:self action:@selector(testAlertView:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonAlertView];
}

- (void)testAlertView:(id)sender{
//構(gòu)造函數(shù)中第一個(gè)參數(shù)是警告框的標(biāo)題,第二個(gè)參數(shù)是警告框的內(nèi)容,第三個(gè)參數(shù)是一個(gè)枚舉值,表示對(duì)話框類型。枚舉值有兩個(gè)ActionSheet表示操作表,Alert表示警示框,默認(rèn)情況下是操作表
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告框" message:@"警告內(nèi)容" preferredStyle:UIAlertControllerStyleAlert];

//UIAlertAction對(duì)象相當(dāng)于一個(gè)按鈕,每往AlertController添加一個(gè)AlertAction就相當(dāng)于添加一個(gè)按鈕。
//此構(gòu)造函數(shù)的第一個(gè)參數(shù)是選項(xiàng)的名稱,第二個(gè)參數(shù)是一個(gè)枚舉值,表示選項(xiàng)樣式,第三個(gè)參數(shù)是參與按鈕動(dòng)作的事件
//第二個(gè)參數(shù)中的枚舉值:Default表示默認(rèn)樣式,粗體顯示標(biāo)題;Cancel,取消按鈕樣式;Destruction,破壞性按鈕樣式,紅色顯示標(biāo)題
    UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"No"
                                                       style:UIAlertActionStyleCancel
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                                                                    NSLog(@"點(diǎn)擊否定按鈕。");
                                                                                                }];
    
    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES"
                                                        style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * _Nonnull action) {
                                                                                                    NSLog(@"點(diǎn)擊確定按鈕。");
                                                                                                 }];
    
    [alertController addAction:noAction];
    [alertController addAction:yesAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

@end

操作表

如果想給用戶多于兩個(gè)選擇那就需要用到操作表。其布局中最下面的是取消按鈕,最容易被點(diǎn)擊到。如果有一個(gè)是破壞性按鈕,將會(huì)被放在最上面,最不容易按到的地方,并且其顏色為紅色。
下面的代碼就是操作表的實(shí)現(xiàn):

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    CGRect screen = [UIScreen mainScreen].bounds;
    
    UIButton *buttonActionSheet = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonActionSheet setTitle:@"Test操作表" forState:UIControlStateNormal];
    CGFloat buttonActionSheetWidth = 100;
    CGFloat buttonActionSheetHeight = 30;
    CGFloat buttonActionSheetTopView = 260;
    
    buttonActionSheet.frame = CGRectMake((screen.size.width - buttonActionSheetWidth)/2, buttonActionSheetTopView, buttonActionSheetWidth, buttonActionSheetHeight);
    [buttonActionSheet addTarget:self action:@selector(testActionSheet:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:buttonActionSheet];
}

- (void)testActionSheet:(id)sender{
    UIAlertController *actionSheetController = [[UIAlertController alloc]init];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
                                                           style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                                                                        NSLog(@"點(diǎn)擊了取消按鈕");
                                                         }];
    UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"破壞性按鈕"
                                                                style:UIAlertActionStyleDestructive
                                                              handler:^(UIAlertAction * _Nonnull action) {
                                                                  NSLog(@"點(diǎn)擊了破壞性按鈕");
                                                              }];
    UIAlertAction *sinaAction = [UIAlertAction actionWithTitle:@"新浪微博"
                                                         style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * _Nonnull action) {
                                                           NSLog(@"點(diǎn)擊了新浪微博按鈕");
                                                       }];
    UIAlertAction *FaceBookAction = [UIAlertAction actionWithTitle:@"FaceBook"
                                                             style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction * _Nonnull action) {
                                                               NSLog(@"點(diǎn)擊了FaceBook按鈕");
                                                           }];
    [actionSheetController addAction:cancelAction];
    [actionSheetController addAction:destructiveAction];
    [actionSheetController addAction:FaceBookAction];
    [actionSheetController addAction:sinaAction];
    
    [self presentViewController:actionSheetController animated:YES completion:nil];
}

@end

操作表中先添加的AlertAction顯示在操作表的上面,后添加的在下面。

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

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,071評(píng)論 4 61
  • 這是一個(gè)方塊的世界。在這里,你可以隨意破壞任何方塊,也可以建筑各種各樣的房子,也可以去擊敗各種怪物(創(chuàng)造模式)。...
    Pokemonster閱讀 699評(píng)論 4 2
  • 攝影師才才閱讀 995評(píng)論 0 2
  • 飛上九萬(wàn)里的高空,看眼下浮動(dòng)的云霧,那種虛無(wú)縹緲的不真實(shí)感再次浮現(xiàn)。直到我踏上磚紅色的土壤,堅(jiān)實(shí)的著陸感讓我才...
    南有喬木怎思清商閱讀 683評(píng)論 0 2
  • 今天一位女性朋友給我打電話,第一句話就是“這幾天好累”,問(wèn)她為什么累,她說(shuō)了幾句我就開(kāi)始犯錯(cuò)誤了。 她是一個(gè)孝女,...
    蘇步哲閱讀 158評(píng)論 0 1

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