標籤:
最近蘋果更新的IOS8 對以前進行了很大的修改, 更新的API也讓人捉急,據說iOS 8的新特性之一就是讓介面更有適應性、更靈活,因此許多視圖控制器的實現方式發生了巨大的變化。比如全新的UIPresentationController在實現視圖控制器間的過渡動畫效果和自適應裝置尺寸變化效果(比如說旋轉)中發揮了重要的作用,它有效地節省了程式員們的工作量。但是同時某些舊的UIKit控制項也同樣發生了許多變化,很多自訂在舊控制項上的控制項發生了詭異的BUG,其實UIAlertView、UIActionSheet (以及它們各自的 delegate protocols) 在 iOS 8 中已經被廢棄了,在你的代碼中按住點擊 UIAlertView 或者 UIActionSheet,你就會看到最上面的注釋:UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.雖然類已經被廢棄了,但在 @availability 屬性中並沒有表達出這一點。UIAlertView 目前還是能用的。更新不可怕,學會了就可以拿來用了.本文將會對Alert Views和Action Sheets發生的改變進行一個大致的介紹.
UIAlertView
隨著蘋果上次iOS 5的發布,UIAlertView 充滿了無底線的讓步,犧牲格式和設計正確性來順應開發人員的喜好。UIAlertView警告框就一直被大量使用,簡單易用,應用情境巨大,一般使用時候常伴隨兩個按鍵,確定&取消.比如:
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"標題" message:@"這個是UIAlertView的預設樣式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];[alertview show];
您也可以通過更改UIAlertView的alertViewStyle屬性來實現輸入文字、密碼甚至登入框的效果。UIAlertViewDelegate協議擁有響應對話方塊視圖的按鈕動作的回調方法。還有當文字框內容改變時,調用alertViewShouldEnableOtherButton:方法可以讓按鈕動態地可用或者不可用。
說了這麼多其實都沒什麼用,因為在IOS8.0之後 ,蘋果已經不建議我們用UIAlertView了,而是新的UIAlertController
UIAlertController
UIAlertController 同時替代了 UIAlertView 和 UIActionSheet,從系統層級上統一了 alert 的概念 —— 即以 modal 方式或 popover 方式展示。
UIAlertController 是 UIViewController 的子類,而非其先前的方式。因此新的 alert 可以由 view controller 展示相關的配置中獲益很多。
UIAlertController 不管是要用 alert 還是 action sheet 方式展示,都要以 title 和 message 參數來初始化。Alert 會在當前顯示的 view controller 中心以模態形式出現,action sheet 則會在底部滑出。Alert 可以同時有按鈕和輸入框,action sheet 僅支援按鈕。
新的方式並沒有把所有的 alert 按鈕配置都放在初始化函數中,而是引入了一個新類 UIAlertAction 的對象,在初始化之後可以進行配置。這種形式的 API 重構讓對按鈕數量、類型、順序方便有了更大的控制。同時也棄用了 UIAlertView 和 UIActionSheet 使用的delegate 這種方式,而是採用更簡便的完成時回調。
<span style="color:#33cc00;"><strong>// 注意這裡的</strong></span><span style="font-family: ‘Helvetica Neue‘, Helvetica, STheiti, 微軟雅黑, 黑體, Arial, Tahoma, sans-serif, serif;"><span style="color:#33cc00;"><strong>preferredStyle的參數是一個枚舉值 <span style="color: rgb(51, 204, 0); font-family: Menlo;font-size:18px; line-height: 28px; white-space: pre; ">UIAlertControllerStyleActionSheet 就是原UIActionSheet</span></strong></span></span><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#33cc00;">typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#33cc00;"> UIAlertControllerStyleActionSheet = 0,</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#33cc00;"> UIAlertControllerStyleAlert</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#33cc00;">} NS_ENUM_AVAILABLE_IOS(8_0);</span></p>
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"標題" message:@"<span style="font-family: ‘Helvetica Neue‘, Helvetica, STheiti, 微軟雅黑, 黑體, Arial, Tahoma, sans-serif, serif;">message</span>" preferredStyle:UIAlertControllerStyleAlert];
<pre name="code" class="objc"><span style="color:#33cc00;"><strong>// 注意這裡的style參數是按鈕樣式,也是枚舉值分別為:</strong></span>
</pre><pre name="code" class="objc" style="color: rgb(255, 102, 0);">UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:nil];[alertController addAction:cancelAction];[alertController addAction:sureAction];
還沒有完事,因為現在警告框已經從一個View"進化"為一個Controller,所以顯示方式也不再是show或者showIn ,而是
[self presentViewController:self.alertController animated:YES completion:nil];
注意,在實驗時一定不要放在
-(void)viewDidLoad
方法裡,(提示一下)可以寫在視圖出現或者自訂一個Button的觸發方法裡.
viewDidLoad
viewDidLoad
IOS開發之IOS8.0最新UIAlertController