So it will come back again in this lesson and you're going to talk about segmented controls.
This is the part of the day you die and the user interface that helps us to develop things such as radio
buttons buttons that they have different conditions and only one of them can be valid at any given time.
So just go ahead into school in school.
I'm going to make my self a new project you're going to close this one first make a new project this
one I'm going to call it sick man said segment that control app in here.
The hat for a storyboard my storyboard.
I want to have a segmented control.
That's one so that's a segment of the control that can have multiple different you know situations are
in this one the default one has only to first and second and then I want to have a label on the label
I'm going to put that in.
Yeah it's just going to show us of which one has been selected.
So that and then finally I'm going to change some of these and the last thing you're going to do in
the interface building is you're going to add these other outlets to our blog.
HYL.
So when I go to the assistant editor a lot my daughter age gets rid of all the comments.
Get myself a little bit more space control drag this one and call it my Sega CD or L and then control
it like my label and call it the Reds label.
So I have two outlets now.
The other thing we have to do is that every time that we tap on this segment that console it switches
from first to second or so on and so forth to other numbers if there were more.
So we have to trigger a function that it's going to hold control like my Sigmond that control and say
instead of an outlet give me an action and the action for it is let's call it a segue or tap on sake.
Now something as important to note this is the last time the events that we were using was touch up
inside because we were tapping on a button.
But this time the value of the segmented control is being changed and we will see how that works.
Why is that.
Someone pressed that.
And now if I jump back into my thought am I have that function or that action that is going to be called
every time the top on my segment.
So this first test to make sure it is actually working to do that I'm first got clean things up in here.
So that's that going to meet any of those guys and in here I'm going to go ahead and say and Aslaug
say segment's was typed on that's all he's going to say every time it happens it is going to say segment
was top 10 and let's run our application and see what do we get.
Once it launches
so I have my concert at the bottom and I actually got a clearance so here every time I tap it says segment's
was top done.
Tap tap tap tap tap tap tap.
It doesn't know which one I have done it just knows that it has been top done.
So it is now left up to us to find out which one top line and to get that we have to get the index of
the Koren's we selected.
Let's see that.
If I go to my storyboard it says this has two segments segment zero.
It's saying First let's change that to hello hello and then I say this second segment segment one.
It says a wall.
So one of them says hello.
One of them says Well of course if you want we could change this and add more to them.
But what is important is to notice this segments they have or they're so the first one is zero like
an array.
The second one is one so on and so forth so that we can use to find out which segment has been currently
selected.
Let's jump back into that.
I think my that and I'm going to go ahead and say integer our current index is the name of my segmented
control which I recorded my sig control going to say well it's my sig sorry my sig control dot select
that segment index the name of that Corinth segment that control on the selected segment index.
So which one is currently selected.
Of course this is an integer and this is an integer.
So if you want to you have to cast it to each other and once they crossed it into an integer.
So now we got the current index that's printed out.
I'm going to say corons index is.
And then I'm going to have that on say current index.
So if you run this application now every time without it has to tell us of which segment we have actually
are currently selected and me clearly my console and then now it says current indexes 1.
Now I said Krant index is zero.
So on and so forth.
Obviously the rest of it is quite straightforward.
For instance like with say if current index is zero then read the label that dot text should show us
hello.
I don't know where are the WMD and the fit every time else our rez is a label d'arte text has to be
Tom wall and that's about that.
So right now everything that gets called it finds out which one has been tapped on based on that makes
a decision simple.
So we can see it in here.
So that's about now.
Another thing we could do is we could obviously change the number of these segments.
We could go ahead and say there are three segments and that's segment 2 or the last one.
Let me just zoom in here a little bit.
This zooming sometimes doesn't work well in video recording.
I hope it works by now.
Hello war are you all.
Yes.
So the last one essentially says I or yes.
Now that's one way of adding them of course you could add them programmatically and they could add them
by essentially dynamically to put them on the fly.
Let's see how we could do that.
I'm going to add two more buttons in here are two buttons essentially.
So that's one of.
Like that again but.
So this is the one button.
It says.
I mean OK it's a little bit larger.
I like it to be nice and tidy when I'm working.
So I'm going to say you remove.
Right.
So my remove has a darker background and then I have another one which actually happens to have a brighter
background.
And this one says that.
So let's go ahead on our two functions for them.
I'm going to go to the assistant editor and into that age I'm going to say this is to remove action
and I don't want an outlet I want an action.
It's important to remember that that's not.
And then I have another one.
This one is called add action
action say Action.
Perfect.
So now I have the two buttons and they both have functions.
If I go with the editor you can quickly have a look at our age.
We have that function or the action that it's called when the top on the segment we have to have one
for the remote but then on the one for the output.
And what do we want to do with them.
So I'm going to go in here get myself a little bit more space and I'm going to come in here and say
in the Remove option do this my segmented controller remove Signum and add index.
Let's say you remove the very first one and it is animated.
All of that's true.
So very simple.
It says take my Sigma the controller remove an object that this index and it is animated.
And that is dynamically moving segments from it.
And now we're going to go to the heart and I'm going to say my segment that control and or sorry insert
segment title and the title I want to have is are Android.
Why not at index of one.
Is it animated.
It is animated.
So one of them removes a segment the other one inserts a segment.
Let's run this code and if it all works well you should be able to not only tap on these items and get
to see different things.
We should also be able to remove that gets rid of one of them and either that adds another one remove
it.
Perfect.
So it all works well up here.
Now I want to show you two really quick things and here one of them is what if our segment that control
our view wanted to actually connect it to the code that functions with the code.
It would actually be a very very simple.
It would be my view that Lord I would have to say my segment of control are the target.
This is exactly like what we did for a.
But then the target is in the center of the action has a selector and the event is just like what this
cost is going to be of value you change.
So it's going to be I don't know why it doesn't autocomplete sometimes.
So you are in control events you like control control.
It doesn't other company because of the brackets.
It's all in here.
You control events are for some reason it doesn't show me the value changed to search for its tight
shot publication venues or a touch of value change.
Perfect.
So that's how we would be doing it programmatically.
So we removed that and that's pretty much about it.
So that's how you segment that control something I intentionally you know left out is there is times
that you know when you call on a function whether it is a segmented contour or a button or anything
the sender is not necessarily the same thing that you're expecting.
So right now this sender is essentially the segmented controlled is saying that object is sending us
to this function and it's of the type which means any kind of object.
So we can't be sure what is that.
In our case we got lucky because we already had an outlet for our segment that control what we did and
here is what we could do.
We could say you are segmented control this segment that's controlled is going to cost you segment that
is controlled out of sender.
I would say send there.
I know it is going to be a guys segment that control.
This is of course of course casting something is not the best idea unless you are absolutely sure that
that is the case and that's the type of it.
Otherwise there is another solution.
So the other solution is this if you want to have an action that you know send is absolutely a segmented
control you could actually write it differently.
I'm going to put that edge.
I'm going to write another option.
You have to do it kind of manually or you could modify an existing one.
I'm going to say be action my say action that's the action that I'm writing and I'm going to say it's
sent there is not of that type I.D. It is you I segmented control.
I'm saying I know that the sender for this action is always you or I segment that control.
And as you can imagine these are all just names.
None of them really matters.
You can call this whatever you want.
You can call sender whatever you want.
And there could be put stg you know or input segment that control or whatever you want to call it.
It doesn't really matter.
Now that I have that I can go to my storyboard and in my story word I could let me add another segment
to the control.
So we kind of get a sense of this as well.
So that's there in here.
I'm going to go ahead and say this action that I have received my sig action.
I'm going to drag it over here.
Say you change the.
All right now in my thought end I have to copy this method.
This function I'm going to go to my dad and say this is the function.
And now that input segment it refers to the segment that is calling this function.
So you could modify your Iby actions whichever way you like.
They are not set on a stone.
The only thing that matters about them is this that they are Iby actions.
Other than that not that it really matters.
You can modify it whichever way you like it.
I am however going to remove all of this which is a little bit outside the scope of this lesson and
then when to go to my main storyboard I'm going to delete that guy and when I go to that age you know
that guy.
But remember that when you're writing methods these things they don't necessarily mean anything they
could be called anything.
And they all saw that type.
You could specifically use it for instance for ease to remove action the action.
Now we know there are going to be you I.
They're absolutely going to be you.
But they're not going to be anything else.
So we could actually change them to you.
I don't remember whether you change here in your adult H.
You have to change it in here in your thought am I as well you can just drop it.
And that's about that.
With that in mind I think this is the end of this lesson and we'll move on to our next one to deal with
images.