NSString * okButtonTitle = @ " OK
ns string * Never button title = @ " Never ";
NSString *laterButtonTitle = @ "也許以後";
//初始化
UIAlertController * alert dialog =[UIAlertController alertcontrollerwithttitle:title message:message preferred style:UIAlertControllerStyleAlert];
//分別進行3次創建操作
uialertaction * later action =[uialertaction actionwith title:laterbuttontitle style:uialertactionstyle default handler:^(uialertaction * action){
//普通鍵
self.userOutput.text = @ "單擊了“也許稍後”";
}];
uialertaction * never action =[uialertaction actionwith title:never button title style:uialertactionstyle破壞性handler:^(uialertaction * action){
//紅色按鈕
self . user output . text = @ " Clicked ' Never ' ";
}];
uialertaction * ok action =[uialertaction actionwith title:ok button title style:uialertactionstyle cancel handler:^(uialertaction * action){
//取消鍵
self.userOutput.text = @ "單擊了“確定”";
}];
//添加操作(順序為呈現的上下順序)
[alert dialog add action:lateral action];
[alert dialog addAction:never action];
[alert dialog add action:ok action];
//呈現警告視圖
[self present view controller:alert dialog動畫:是完成:無];
}
這三個按鈕代表三種不同類型的按鈕,即默認按鈕(正常)、銷毀按鈕(紅色)和取消按鈕(粗體)。從代碼上看,它實際上是在最後壹個的基礎上增加了三個UIAlertAction,然後分別設置不同的樣式,效果如下:
3.帶有輸入框的提醒視圖
如何添加輸入框?新的iOS 8提供了相應的接口,使得添加壹個輸入框就像添加壹個鍵方法壹樣簡單。這裏是在1方法的基礎上改變的。
[目標]查看純文本
-(I action)doAlertInput:(id)發件人{
//準備初始化配置參數
NSString *title = @ "電子郵件地址";
NSString *message = @ "請輸入您的電子郵件地址:";
NSString * okButtonTitle = @ " OK
//初始化
UIAlertController * alert dialog =[UIAlertController alertcontrollerwithttitle:title message:message preferred style:UIAlertControllerStyleAlert];
//創建壹個文本框
[警報對話addtextfieldwithconfigurationhandler:^(uitextfield *文本字段){
textField.placeholder = @ "您的電子郵件";
textfield . secure text entry = NO;
}];
//創建操作
uialertaction * ok action =[uialertaction actionwith title:ok button title style:uialertactionstyle default handler:^(uialertaction * action){
//讀取文本框的值並顯示出來。
UITextField * user email = alert dialog . textfields . first object;
self . user output . text = user email . text;
}];
//添加操作(順序為呈現的上下順序)
[alert dialog add action:ok action];
//呈現警告視圖
[self present view controller:alert dialog動畫:是完成:無];
}
在創建操作之前創建壹個文本框,以便下列鍵可以操作文本框的內容。創建文本框只是壹個簡單的方法。如果要創建更多的文本框,可以多次使用該方法。程序效果如下:
提醒圖表
與第二種和第三種方法相比,創建提醒圖表是小菜壹碟。因為與1方法相比,只需要更改壹個參數,即用於創建UIAlertController實例的參數UIAlertControllerStyleAlert更改為UIAlertControllerStyleAction Sheet,其他都不需要更改。
[目標]查看純文本
-(I action)doActionSheet:(id)發件人{
//準備初始化配置參數
NSString *title = @ "選擇了警報按鈕";
NSString *message = @ "我現在需要妳的註意!";
NSString * okButtonTitle = @ " OK
ns string * Never button title = @ " Never ";
NSString *laterButtonTitle = @ "也許以後";
//初始化
UIAlertController * alert dialog =[UIAlertController alertcontrollerwithttitle:title message:message preferred style:UIAlertControllerStyleActionSheet];
//分別進行3次創建操作
uialertaction * later action =[uialertaction actionwith title:laterbuttontitle style:uialertactionstyle default handler:^(uialertaction * action){
//普通鍵
self.userOutput.text = @ "單擊了“也許稍後”";
}];
uialertaction * never action =[uialertaction actionwith title:never button title style:uialertactionstyle破壞性handler:^(uialertaction * action){
//紅色按鈕
self . user output . text = @ " Clicked ' Never ' ";
}];
uialertaction * ok action =[uialertaction actionwith title:ok button title style:uialertactionstyle cancel handler:^(uialertaction * action){
//取消鍵
self.userOutput.text = @ "單擊了“確定”";
}];
//添加操作(順序為呈現的上下順序)
[alert dialog add action:lateral action];
[alert dialog addAction:never action];
[alert dialog add action:ok action];
//呈現警告視圖
[self present view controller:alert dialog動畫:是完成:無];
}
這很簡單,和第二種方法很像,效果如下圖:
5.播放系統聲音、提醒聲音和振動設備。
在iOS 8中,調用聲音的方法略有改變,不能像以前那樣獲取系統聲音文件的soundID。所以這裏直接調用soundID值來調用對應的聲音,註意震動還是正常調用kSystemSoundID_Vibrate常量:
[目標]查看純文本
-(I action)do sound:(id)發件人{
//播放系統聲音
AudioServicesPlaySystemSound(1005);
}
-(I action)doAlertSound:(id)發件人{
//播放提醒聲音
AudioServicesPlayAlertSound(1006);
}
-(I action)do vibration:(id)發件人{
//執行振動
AudioServicesPlaySystemSound(kSystemSoundID _ Vibrate);
}
這樣就可以在iOS 8中提示用戶。