做家具的企業(yè)網(wǎng)站最吸引人的營銷廣告詞
目錄
- 前言
- 關(guān)于UIAlertController
- 具體操作及代碼實現(xiàn)
- 總結(jié)
前言
??在UI的警告對話框的學習中,我們發(fā)現(xiàn)UIAlertView在iOS 9中已經(jīng)被廢棄,我們找到UIAlertController來代替UIAlertView實現(xiàn)彈出框的功能,從而有了這篇關(guān)于UIAlertController的學習筆記。
關(guān)于UIAlertController
??UIAlertController 是 iOS SDK 中提供的一個強大且靈活的類,它可以用來顯示警告框或者動作表。UIAlertController 取代了早期的 UIAlertView 和 UIActionSheet 類,提供了更加統(tǒng)一和靈活的界面。
具體操作及代碼實現(xiàn)
//創(chuàng)建一個UIAlertController對象
//P1:彈出框的標題 P2彈出框的內(nèi)容
//P3:彈出的警告框的樣式為UIAlertControllerStyleAlert(即中心彈出的警告框)
UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"標題" message:@"這是消息" preferredStyle:UIAlertControllerStyleAlert];//添加“確認”動作按鈕到控制器上
//P1:標題文字 P2:動作樣式,有三種動作樣式:常規(guī)(default)、取消(cancel)以及警示(destruective)
//P3:用戶選中并點擊該動作時,所執(zhí)行的代碼
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {// 用戶點擊確認按鈕后執(zhí)行的代碼
}];
//將動作按鈕添加到alertController視圖上
[alertController addAction:defaultAction];//添加“取消”動作按鈕到控制器上
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {// 用戶點擊取消按鈕后執(zhí)行的代碼
}];
//將動作按鈕添加到alertController視圖上
[alertController addAction:cancelAction];//將警告框顯示出來
[self presentViewController:alertController animated:YES completion:nil];
??這個示例創(chuàng)建了一個 UIAlertController,并設(shè)置了標題、消息以及樣式。然后,我們創(chuàng)建了兩個 UIAlertAction,一個是默認的操作,另一個是取消操作(有三種動作樣式:常規(guī)(default)、取消(cancel)以及警示(destruective))。這兩個操作都有處理器,所以當用戶點擊這些按鈕時,會執(zhí)行相應(yīng)的代碼塊。最后,我們使用 presentViewController:animated:completion: 方法來顯示警告框。
??將以上代碼放進xcode的"ViewController.h"文件的viewDidLoad函數(shù)中,運行后你會發(fā)現(xiàn),模擬器屏幕上無任何顯示,這是因為:
在 iOS 開發(fā)中,警告對話框(UIAlertController)不能直接被呈現(xiàn),必須在某個存在的視圖控制器(UIViewController)上呈現(xiàn)。這是因為UIAlertController實際上是一個視圖控制器,而所有的視圖控制器都需要一個父視圖控制器才能被用戶看到。
??為了看到警告框的效果,我們另外創(chuàng)建一個UIButton對象,用來觸發(fā)警告框。
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];btn.frame = CGRectMake(100, 100, 80, 80);btn.backgroundColor = [UIColor cyanColor];[self.view addSubview:btn];[btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
將UIAlertController相關(guān)操作放在btn的press函數(shù)里即可。完整代碼如下:
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];btn.frame = CGRectMake(100, 100, 80, 80);btn.backgroundColor = [UIColor cyanColor];[self.view addSubview:btn];[btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
}- (void) press {//創(chuàng)建一個UIAlertController對象//P1:彈出框的標題 P2彈出框的內(nèi)容//P3:彈出的警告框的樣式為UIAlertControllerStyleAlert(即中心彈出的警告框)UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"標題" message:@"這是消息" preferredStyle:UIAlertControllerStyleAlert];UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"確認" style:UIAlertActionStyleDefault handler:nil];[alertController addAction:defaultAction];UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];[alertController addAction:cancelAction];[self presentViewController:alertController animated:YES completion:nil];
}@end
按下btn按鈕后的運行結(jié)果:
總結(jié)
??當我們需要顯示一個警告對話框時,我們要在當前的視圖控制器上呈現(xiàn)它;如果當前不在視圖控制器中,但是需要顯示警告對話框,我們需要獲取到當前的視圖控制器,或者創(chuàng)建一個新的視圖控制器來呈現(xiàn)警告對話框。