iOS7、iOS8 Alert 更新轉換
UIAlertView | UIAlertController |
---|---|
iOS7 在UI外觀帶來相當大的變格,但在iOS8上面則是將Framework增強許多,也將許多的Framework進行整合,然而UIAlertView則是其一,在iOS8之後,Apple將UIAlertView、UIActionSheet進行整合,取而代之的為UIAlertController
,這帶來的好處則是在iPad、iPhone不用再針對其特性來撰寫專用的程式,這些底層都會幫你處理完成,雖然是如此,但還是要在舊版本上面做一些過度期、相容的動作。
iOS7 UIAlertView
iOS7 下要跳出Alert視窗的程式碼如下:
//-----------start----------- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message For iOS 7" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil]; [alertView show]; //------------end------------
iOS8 UIAlertController
iOS 8 使用UIAlertController 來取代UIAlertView、UIActionSheet,使用方式也很容易:
//-----------start----------- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message For iOS8" preferredStyle:UIAlertControllerStyleAlert]; // Close UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:alertAction]; [self presentViewController:alertController animated:YES completion:nil]; //------------end------------
版本判斷與整合
//-----------start----------- if ([UIAlertController class]) { // iOS 8以上使用 UIAlertController UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Title" message:@"Message For iOS8" preferredStyle:UIAlertControllerStyleAlert]; // Close UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleCancel handler:nil]; [alertController addAction:alertAction]; [self presentViewController:alertController animated:YES completion:nil]; } else { // iOS7 以下 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message For iOS 7" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil]; [alertView show]; } //------------end------------
目前iOS 8 還是能沿用iOS 7所提供的做法,所以在撰寫時依照你自已的考量自行決定哪個方式比較適合。
延伸閱讀
UIAlertController Changes in iOS 8