Displaying Alerts in iOS 8
- UIAlertView and UIActionSheet: Deprecated
- Welcome, UIAlertController
- Obtaining Text Inputs
- Adaptive Rendering on the iPhone and iPad
- Summary
With the launch of the iPhone 6 and iPhone 6 Plus, Apple also released the iOS 8 platform. Among the many updates in this version of iOS are some subtle changes that affect how alerts are displayed. In this article, I will discuss the new UIAlertController class and the classes that it replaces.
UIAlertView and UIActionSheet: Deprecated
If you've ever programmed iOS apps, no doubt you're familiar with the UIAlertView class. You most likely used this class in your first iOS “Hello World!” application. In iOS 8, the UIAlertView class is still available, but deprecated. You should avoid using it, as future versions of iOS might not support it.
The following example shows the UIAlertView in iOS 8, written in Swift:
var alertView = UIAlertView( title:"Hello, iOS8", message:"I am deprecated!", delegate:self, cancelButtonTitle:"OK", otherButtonTitles:"Cancel") alertView.show()
Figure 1 shows how the UIAlertView class looks.
Figure 1 The venerable UIAlertView class, deprecated in iOS 8.
To learn which button the user has tapped, you need to implement the UIAlertViewDelegate protocol and implement the following method:
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) { println("Button click: \(buttonIndex)") }
Like the UIAlertView class, the UIActionSheet class is deprecated in iOS 8. The following code creates an action sheet in iOS 8:
var actionSheet = UIActionSheet( title: "Are you sure?", delegate:self, cancelButtonTitle: "Cancel", destructiveButtonTitle: "Delete", otherButtonTitles: "Delete with backup") actionSheet.showInView(self.view)
Figure 2 shows the Action Sheet in action.
Figure 2 The UIActionSheet class is deprecated in iOS 8.