Displaying User Alerts

In this section, you are going to learn about user alerts and the different ways of configuring and displaying them. User alerts can provide your application with a better user experience, so you will use them fairly often.

Alerts are often used to warn users that an important action is about to happen and perhaps give them the opportunity to cancel that action. When you want to display an alert, you create an instance of UIAlertController with a preferred style. The two available styles are UIAlertControllerStyle.actionSheet and UIAlertControllerStyle.alert (Figure 11.9).

Figure 11.9  UIAlertController styles

Collage of two screenshots depicts the usage of two different UIAlertController styles.

The .actionSheet style is used to present the user with a list of actions from which to choose. The .alert type is used to display critical information to require the user to decide how to proceed. The distinction may seem subtle, but if the user can back out of a decision or if the action is not critical, then an .actionSheet is probably the best choice.

You are going to use a UIAlertController to confirm the deletion of items. You will use the .actionSheet style because the purpose of the alert is to confirm or cancel a possibly destructive action.

Open ItemsViewController.swift and modify tableView(_:commit:forRowAt:) to ask the user to confirm or cancel the deletion of an item.

override func tableView(_ tableView: UITableView,
                        commit editingStyle: UITableViewCellEditingStyle,
                        forRowAt indexPath: IndexPath) {
    // If the table view is asking to commit a delete command...
    if editingStyle == .delete {
        let item = itemStore.allItems[indexPath.row]

        let title = "Delete (item.name)?"
        let message = "Are you sure you want to delete this item?"

        let ac = UIAlertController(title: title,
                                   message: message,
                                   preferredStyle: .actionSheet)

        // Remove the item from the store
        itemStore.removeItem(item)

        // Also remove that row from the table view with an animation
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }
}

After determining that the user wants to delete an item, you create an instance of UIAlertController with an appropriate title and message describing what action is about to take place. Also, you specify the .actionSheet style for the alert.

The actions that the user can choose from when shown an alert are instances of UIAlertAction, and you can add multiple ones regardless of the alert’s style. Actions are added to the UIAlertController using the addAction(_:) method.

Add the necessary actions to the action sheet in tableView(_:commit:forRowAt:).

...
let ac = UIAlertController(title: title,
                           message: message,
                           preferredStyle: .actionSheet)

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
ac.addAction(cancelAction)

let deleteAction = UIAlertAction(title: "Delete", style: .destructive,
                        handler: { (action) -> Void in
    // Remove the item from the store
    self.itemStore.removeItem(item)

    // Also remove that row from the table view with an animation
    self.tableView.deleteRows(at: [indexPath], with: .automatic)
})
ac.addAction(deleteAction)
...

The first action has a title of Cancel and is created using the .cancel style. The .cancel style results in text in a standard blue font. This action will allow the user to back out of deleting an Item. The handler parameter allows a closure to be executed when that action occurs. Because no other action is needed, nil is passed as the argument.

The second action has a title of Delete and is created using the .destructive style. Because destructive actions should be clearly marked and noticed, the .destructive style results in bright red text. If the user selects this action, then the item and the table view cell need to be removed. This is all done within the handler closure that is passed to the action’s initializer.

Now that the actions have been added, the alert controller can be displayed to the user. Because UIAlertController is a subclass of UIViewController, you can present it to the user modally. A modal view controller takes over the entire screen until it has finished its work.

To present a view controller modally, you call present(_:animated:completion:) on the view controller whose view is on the screen. The view controller to be presented is passed to it, and this view controller’s view takes over the screen.

...
let deleteAction = UIAlertAction(title: "Delete", style: .destructive,
                        handler: { (action) -> Void in
    // Remove the item from the store
    self.itemStore.removeItem(item)

    // Also remove that row from the table view with an animation
    self.tableView.deleteRows(at: [indexPath], with: .automatic)
})
ac.addAction(deleteAction)

// Present the alert controller
present(ac, animated: true, completion: nil)
...

Build and run the application and delete an item. An action sheet will be presented for you to confirm the deletion (Figure 11.10).

Figure 11.10  Deleting an item

Screenshot of the “Delete” action in the Homepwner app.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset