Hi, Alex here from rebeloper.com and welcome back to the channel and welcome back to
this epic tutorial series on DuckUI. Now what is DuckUI. SwiftUI for UIK it you
heard that right SwiftUI for UIKit. Well kind of and today we are going
to talk about alert controllers. So let's jump right into the code.
Okay! In the previous episodes we have been talking about logging and debugging well
loging in the console. So you know how to debug your code and debug on your UIViews
on the screen, so let me just show you how that looks like on the
screen. So you can debug visually what you have put out that there in your UI.
So today we are going to move even further. So now you do have all of those
information but most probably you will need to alert your users too. So that's
why the alert controller comes in. So basically that is a simple UIAlertController
and let me just show you by example it will be super fast and easy.
So let me just see what we got here. Let's add a button right down here why
not and we just leave the debugging there. If you don't know what I'm talking
about go ahead and take a look at the previous
episode in this playlist you can have a take a look at the description for the
details of the links for the playlist you know. So let's move forward, let's
create the button real quick. So let button that will be a UIButton, okay and
yeah that is going to be just a UIButton. Let's add a text to it and the
text will be tap me and let's see let's have background
color maybe, background color, yeah. Let's use
our and D_Colors let's say dark, oh
Xcode 11 first of all and dark and if you don't know what I'm talking about
take a look at the previous episodes I go in the detail what the color is and
so on. So let's add a corner radius to let's say 5 and as you can see this
is how easy it is to construct a button. So, yeah there we go.
Let's that corner radius, yeah and it should be fine, okay. There we have our
button. So let's add it under our label that is inside the horizontal stack, so I
will just add it here so button, okay. Let's maybe add some insects too.
So insetting and I will insetting to 12. Now let's see how that looks.
Let;s hit command R or that play button and see how all of that looks like.
Tap me, there we go. Really, really useful and fun to create
UI as simple as that with DuckUI. Now if you recall or if you just go ahead and
take a look at my other video on buttons we need to create observable and observe
on our button and that is pretty simple. So button dot reactive dot tab and
observe next, there we go. Observe next and we are just going to delete this for
now because we maybe we want to add a weak self there but we are not going to
do that. Finally we want to dispose this in the
bag I won't go into details why this is I am planning all of this in the
episode of buttons. Well basically what we want
to do here is show an alert. So for now let's use our trusty log and let's have
a success log here. Again and if this is brand new to you go ahead and
take a look at other videos they are titled real nice-like long and button so
you can find everything that you are searching for. So tapped button, okay and
yeah that should be it we have added three, four, five lines of code, well
basically seven lines of code but this could be numbered as only one, but just
for readability we have these two extras and we do have that button. So let's hit
command R or again in the play button and let's tap this and it says tap
button. Let's tap it again. Great, we have tabbed that button a
couple of five and it looks really really nice. So let's jump into today's
problem. So let's solve it we want to show one alert. So let's do that I will
remove this it's a little bit of clutter and you know if you work with alerts you
have to write a lot. So you have to initialize it, you have to create actions
for it, but not now we are just going to use the simple D_AlertController and
dot and we have a few. So a simple one with a style and that is alert or action
sheet. Title and message we have title message with alert actions of course
with a completion handler if you wanted to and we have one that has an array of
UITextFields, yeah you could use this for collecting let's say usernames and
passwords or emails or something like that. So I will go with the and that is
straightforward after it's simply adding a UITextField
as we would add UIAlertAction. So I will use this second one so the alert
style let's go for alert and the title let's have this warning
maybe. Warning the message this is our message, okay and let's add some actions
and these are an array or UI of UIAlertActions. So let's do that. Let's create an
okay action and the console action. It doesn't really matter you can't use
anything I believe that you should have at least two but if you only want one
then you should use the first one that has only title and message and it says
okay. So, yeah go ahead and check those out how did it look. So all right let's
see okay action and that is a UIAlertAction with the title of let's say, okay.
Style to use default here handler and inside the handler we have an action
coming back I really use that I will just print something out like a log
let's say success. You could basically use this for anything you could navigate
away from the scene or something else. So that okay. One last thing I want to
create a cancel button, okay. Let cancel action.
Just make that capital A there and that will be of type UIAlertAction title
cancel of course and the style and let's talk a bit a bit about style.
The cancel is basically bold. The default is normal and the descriptive is red.
So I just choose cancel there and let's add nil for the handler because we
don't want to do that I have anything to about that. So let's just copy and paste
those two actions inside our actions. There we go. The cancel action too and
basically I don't want to do anything after we have presented our alert
controller. So yeah let's just build and run and see how that looks and then I
would just move on to showing you some action sheets warning. This is our
message. We can cancel out. Let's just cancel out nothing happens and let me
just tap that again. Let's click on okay there and it says tapped, okay, great.
Let's just, let me just show you the other type of destructive, there we go.
This is kind of showing the user that okay this is serious, okay we got that, okay.
And finally let's choose another style let's see here dot action sheet, not
alert. Let's hit command R and yeah with just a few lines of code there you have
your layer. You can navigate the way you can do whatever you want well action is
selected. So there we go. Warning this is our message, okay, cancel.
Pretty straightforward. If you would have added let's let's add another one, okay.
Action and let's say not sure maybe not sure, maybe we're not sure action, not
sure and this will be a normal one. I believe that's, no it's a default.
It's called default and tapped not sure and success I would just use warning.
Okay! Let's build them, oh sorry we need to add that not sure action to the list
of our actions there. Great, there we go. Not sure action. Let's hit command R or
that play button and hit tab me, there we go, okay.
Not sure, if we tap on not sure, tap on not sure, okay.
taped on okay and cancel, okay. That does nothing, okay.
Let me just show you that on the alert. Hit command R, yeah and basically
that is it. Let's just test this out. Tap me, okay, not sure, cancel, really, really
awesome and really simple to implement. If you have been ever working
with UIAlertControllers you can do know that it's real pain and as a
bonus this takes care of the iPad crash. You don't need to know about it if you
do know about it then I'm pretty sorry that you encountered that it's a real
pain to fix but this works on iPads too. Okay! I hope you enjoyed this video.
If you did go ahead and give me a thumbs up, subscribe to the channel and hit that
notification bell because in the next video we are going to talk about HUDs
and you don't want to miss that out. So hit that notification bell to get
notified instantly when I put that out and the usual I will see you in the next one.