標籤:
進行iOS開發過程中,不可避免的使用到各種提醒,來提醒使用者當前操作,或是為了警告,或是為了資料緩衝。
本文介紹了使用 UIAlertController和UIAlertAction兩個類,完成三種狀態的提醒。
這裡首先使用UIAlertController建立一個提示對話方塊,按照Factory 方法即可快速建立,參數UIAlertControllerStyle只有一種樣式:UIAlertControllerStyleAlert。
填寫完提示的標題和內容後,就可以使用UIAlertAction建立一個具體的按鈕行為了。參數UIAlertActionStyle有三種樣式, UIAlertActionStyleDefault(普通)、UIAlertActionStyleCancel(取消)、UIAlertActionStyleDestructive(警告(破壞性的))。預設狀態是正常藍色字型,取消狀態時字型加粗,而警告狀態字型則會變為紅色。當只添加一個行為對象時,行為對象會顯示在UIAlertController對象的下面,添加2個時,並排顯示,添加3個及以上者,按列顯示。
你可以很方便的在任意一個事件響應函數中,添加以下代碼,並在塊語句中添加當使用者選擇相應的選項時執行的語句。
//使用UIAlertController建立一個提示對話方塊,只有標題和資訊 //UIAlertControllerStyle只有一種樣式:UIAlertControllerStyleAlert //使用UIAlertAcion建立具體的行為,同時添加三個則按列顯示 //UIAlertActionStyle有三種樣式,普通、取消、警告(破壞性的) //UIAlertActionStyleDefault、UIAlertActionStyleCancel(字型加粗顯示)、UIAlertActionStyleDestructive(字型紅色顯示) UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"標題" message:@"messagebox" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { }]; UIAlertAction *action2= [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; UIAlertAction *action3= [UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { }]; [alert addAction:action1]; [alert addAction:action2]; [alert addAction:action3]; [self presentViewController:alert animated:YES completion:^{ }];
ios警告與提示對話方塊