Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Earlier, when we implemented the vend functionality, we wrote incomplete error handling code. In this video, let’s inform the user an error has occurred by displaying an alert view.
-
0:00
Okay, welcome back.
-
0:02
Earlier when we implemented the event functionality,
-
0:05
we wrote incomplete error handling code.
-
0:07
So if we go all the way up to the purchase method,
-
0:10
you'll see that there's just an empty catch block.
-
0:13
We're throwing errors when they occur, but we're not doing anything to handle them.
-
0:18
Let's fix that now by showing our users an alert if something goes wrong.
-
0:23
An alert view is represented by the class, UIAlertController.
-
0:28
And it has a simple init method that takes
-
0:30
all the information we need to construct and display an alert.
-
0:34
Since we have three different alerts that we're going to display depending on
-
0:38
the error the user has run into, let's create a single method that can create and
-
0:42
display different alert views for us.
-
0:45
I'm going to add this at the end of the vend section here.
-
0:48
So after the updateQuantity method.
-
0:53
And we'll call this method showAlert.
-
0:59
Creating an alert view is really simple.
-
1:01
Again we're going to use the UIAlertController class, and
-
1:04
it has a initializer method that we can easily use to pass some arguments.
-
1:09
So I'll say let alertController.
-
1:13
Now we're gonna create an instance of UIAlertController and
-
1:19
we use this initializer method that takes a title, message, and a style.
-
1:23
For the first one, we'll say the title is Out of Stock.
-
1:31
The message which will be displayed under the title will say,
-
1:34
This item is unavailable.
-
1:39
Please make another selection.
-
1:43
And for the style we're going to say alert because we're displaying an alert.
-
1:48
Now this preferred style here, this parameter is of type UIAlertController
-
1:53
style, which again is an enum with members to represent the different alert styles.
-
1:58
So that's it.
-
1:58
Now we have an alertController.
-
2:00
How do we show it though?
-
2:02
To get one view to show another view,
-
2:05
we actually designate that task to the viewController,
-
2:08
because again, a viewController controls that view hierarchy.
-
2:11
So we're going to ask this viewController to show another view,
-
2:16
which is also in turn controlled by a separate controller.
-
2:19
And we're going to shoot on top of the current view.
-
2:22
And we do that using a method, present.
-
2:26
Present takes a viewController to present.
-
2:29
So we'll pass in the alertController, and we do want to animate this, so
-
2:34
we'll say, true.
-
2:36
And then for the completion handler which is some code that is executed
-
2:40
after we've presented the view.
-
2:42
For now we'll just say no because we don't want it to do anything.
-
2:45
Presenting and showing views is not a simple task, but a lot of the heavier
-
2:49
lifting is done for us by UIkit and the methods implemented in UIViewController.
-
2:55
Again note that I'm saying present the alertController, not the alert view,
-
3:00
because the alertController then goes ahead and
-
3:02
controls that segment of views, its own view hierarchy.
-
3:06
This style of presenting a viewController from a containing
-
3:09
viewController is used to implement a modal interface.
-
3:14
A modal view is a child view that is displayed on
-
3:17
top of the main view to perform a different set of actions.
-
3:22
Modal views prevent the user from interacting with the underlying view
-
3:25
until they perform the action required by the modal view.
-
3:30
You should be familiar with this.
-
3:32
When an alert pops up on your phone, you can't simply go back to using the regular
-
3:36
app until you either acknowledge or dismiss that alert.
-
3:39
This is intentional.
-
3:41
You display a modal of you when you want the user to take it related but
-
3:46
separate set of actions, that then determines what happens in the main view.
-
3:51
Modal views can be full screen as we will see shortly, or
-
3:55
they can take up just a bit of the screen like an alert, which is also a modal view.
-
4:00
Either way, they prevent input on the underlying view.
-
4:03
So yeah ,as we mentioned, by default if you ask a viewController to
-
4:08
present another viewController, it displays that set of views modally.
-
4:14
An app has a view hierarchy, and ours currently looks like this.
-
4:19
Presenting a modal view makes that view appear directly on top of the view stack
-
4:23
like this.
-
4:24
This seems intuitive but it's important to understand how the view stack
-
4:28
changes as we present and dismiss views.
-
4:31
As we learn about other modes of navigation,
-
4:33
you'll see that the stack behaves differently.
-
4:36
Okay, now that we can show one alert, let's modify our error handling code.
-
4:42
Now this specific alertController that we created informs the user that we're out
-
4:46
of stock.
-
4:46
So we'll use this if we hit the out-of-stock error.
-
4:50
So go all the way back up to the purchase method.
-
4:53
And right here in the do catch block, we'll catch on the VendingMachineErrors,
-
5:00
outOfStock case, and then in here we will simply say show alert.
-
5:07
Now you'll get an error once we've change the code here, and that's because now by
-
5:12
catching on a specific error, our do catch clause isn't exhaustive.
-
5:18
We're not handling any other error cases, for
-
5:20
example what happens if we hit the vending machine errors, insufficient funds, or
-
5:24
invalid stock, or invalid selection I mean, or any other set of errors.
-
5:29
For that we're going to add an empty generic catch block, for
-
5:32
now we'll fix this.
-
5:35
But this way it sort of makes our error handling exhaustive, right?
-
5:38
So if we catch on this specific error,
-
5:40
we're going to execute the code inside this clause which is showAlert,
-
5:46
any other error and we'll go here, which for now does nothing.
-
5:49
Again this isn't good practice but we're going to fix this.
-
5:52
So now let's run the app again.
-
5:55
So let's try that again, let's select an item, and a max on the quantity.
-
6:00
And now if we hit Purchase, you'll see that we get an Out of Stock error.
-
6:03
Look at that.
-
6:04
So we have an alert here.
-
6:06
The look and presentation style of the alert is controlled by the system, so
-
6:11
we're just using the defaults here.
-
6:13
Since this view's presented modally, we can't interact with the underlying views.
-
6:18
But we don't have a way to dismiss this either.
-
6:21
So let's stop the app, and figure out how to do that in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up