In the new WatchKit, we can show alert and action sheet with
It’s obvious that the title and the message are the messages to the user.
There are three styles: WKAlertControllerStyleAlert, WKAlertControllerStyleSideBySideButtonsAlert and WKAlertControllerStyleActionSheet. As the name indicates, the first and the second are for alert and the last one is for action sheet. Let’s see the screenshots for different styles.
In WKAlertControllerStyleAlert style, the buttons are simply arranged vertically as the screenshot shows.
In WKAlertControllerStyleSideBySideButtonsAlert style, the buttons are arranged side by side. The number of buttons are limited up to two.
WKAlertControllerStyleActionSheet is the style for action sheet. The difference is the cancel button is in the top left corner to dismiss the sheet.
The infomation like title, style and handler of the button is in WKAlertAction. There are three styles for button : WKAlertActionStyleDefault, WKAlertActionStyleCancel, WKAlertActionStyleDestructive. The default is for most scenarios while cancel is for dismiss the controller without doing anything. The destructive style with red color text is to alert the user about the destructive actions like deleting data.
When one action is selected, the corresponding handler is invoked. The handler is a simple block without parameters and returns.
typedef void (^WKAlertActionHandler)(void)
Here is an example to show alert controllers in three different styles. In each controller, we will show default, cancel and destructive actions.
Build and run!