1. In the last lesson, we have already implemented this national flag selection, right?
Then, there is our date selection control, UIDatePicker
Custom View, are you already familiar with it?
So, we won’t say any more,
Next, let’s take a look at the second control.
UIDatePicker,
In the morning, we just gave a brief introduction. In the afternoon, we used it to do some things.
Well, first of all, it’s the same. Let’s take a look at its case first.
Click this "13-Day 1 (application startup principle).xcodeproj"
Open this project:
Then, select the 4th Target: UIDatePicker
command + R:
Is there a text box here? Then, after clicking on this text box:
Do you see, usually after we click on the text box, what should it look like?
What is displayed is a keyboard, right?
But what is displayed here? Are they some dates? You can choose.
1) Click cancel: nothing happens
2) Click Confirm:
Put the corresponding time on it,
The interface below is implemented by our UIDatePicker.
What about the one in the middle? I’ll explain it to you later.
Okay, first of all, let’s take a look,
2.UIDatePicker control:
UIDatePicker: The main purpose is to facilitate users to select dates and times.
What I just saw is a date?
When you go down to write, you can also consider using some time, that’s fine too.
Usually presented as inputView of TextField,
What does the inputView of TextField look like?
For example, this is a mobile interface:
Above, there is a text box:
By default, after we click on this text box, it will pop up a keyboard for you.
As for the keyboard, there are probably just this:
Everyone knows the height of the keyboard, it is probably 216.
That’s the height of 216,
By default, when you click on the text box, a keyboard will pop up, and then you can enter text, numbers, etc.
But what if you improve this text box's inputView, our inputView here?
That's not it,
What about this word, inputView:
input: What does it mean, input?
View: Well, it’s just an input interface.
Input view, input interface:
Then, the view is our control, right?
UIView type controls,
Then, the input view, an input control,
If you put it as inputView, set it up,
After that, what interface do you put on it?
Then what interface does it display?
You hand UIDatePicker to it,
Then, when you click on the text box, the DatePicker pops up.
Instead of the system keyboard,
Also, inputView, there is another thing,
inputAccessory
Let me move this thing out.
inputAccessoryView: What is this?
This is on the keyboard, with a strip tied to it:
A strip tied to the keyboard:
Accessory: Have you seen this before? Accessory, what is it?
Is it an auxiliary view?
that inputAccessoryView: input auxiliary view,
Just draw it here, probably on our keyboard, probably at such a high position:
If you set a view, estimate its height.
Then, it can be displayed. Of course, if you increase the height, it may become reality, or it may become higher:
Then, there is such a view,
When you click on the text box, both of them will pop up,
If you set inputView and inputAccessoryView at the same time,
As for the two of them, they just popped up together.
If you only set the inputView, then only the keyboard is replaced.
In other words, the "Cancel, Confirm" you just saw is placed on top of it.
Let’s come back and continue:
This UITextField is introduced to you here.
Well, this is about usage, so I won’t talk about it.
I’ll talk about it later when I write the code,
Talk to you later,
4.NSDateFormatter: Have we used it before?
So, here we don’t look any further.
1) If you convert date to string: there is a [formatter stringFromDate:]
Date: Is it just the date?
String: Well, is it just a string?
stringFromDate: Convert from date to string,
2) The return is the same: [formatter dateFromString:]
Convert a string into a date,
Right,
Okay, those are the two points. Next, our goal is to implement a custom keyboard for this text box:
However, I need to add something to this above: "cancel and confirm".
What about the one above?
1) First: "Cancel, Confirm", is it a commonly used button?
But, where should I put this button? In the inputAccessoryView of the text box,
However, it is not put directly on it.
It requires the use of another control,
Let's take a look together,
2. Create a new project
1. Select this: New-Project,
Select this Single View Application:
Name: 08-custom keyboard datePicker:
Okay, let’s go to Main.storyboard first:
Let’s experience it first:
Let me show you those two strips first:
Where are the two words "Cancel and Confirm" placed?
I put this Window:stay in Front:
I put this window at the front.
Okay, first of all, "Cancel and Confirm", these two things are not the Button we commonly use.
We need to use a new control called: UIButtonItem.
There is nothing difficult about this thing. It is just a button, but the type is different:
See no: UIButtonItem,
Just use it as a button, no problem.
OK, just a button,
Click it, and there will be some operations. However, it has a characteristic, that is, it cannot be included in any View. It needs to be placed in a UIToolbar or UINavigationItem.
1) This UINavigationItem: You don’t have to worry about it, we will learn this tomorrow,
This is a navigation bar,
2) This UIToolbar: This is what we have here:
Use this thing behind this,
A gray, light gray effect,
2. Then, let’s create a UIToolbar first and show it to everyone.
So UIToolbar: Which one is UIToolbar?
This is our thing with Edit,
Did you see: UIToolbar,
It's not difficult at all, it's just a strip.
Then, inside, there are some function buttons.
However, this button does not mean that you can move any button upward.
One must be: UIButtonItem,
Because the things placed are all one: instance of UIButtonItem,
The toolbar items it writes: are actually the UIButtonItems inside.
3. Let’s look at them one by one. Let’s look at this UIToolbar first:
Drag UIToolbar: up,
Put it here:
Put it directly in here,
Do you see it:
This is a UIToolbar,
As for the one next to it, I put it in the back.
This is UIToolbar: the abbreviation is Toolbar, do you see it?
It’s not difficult, it’s just a View.
However, the system packaged it for us,
Abbreviation: called UIToolbar,
And what about these things inside:
It's not an ordinary button, but it's a ButtonItem.
Take a look at its type:
See no: UIBarButtonItem:
No problem, it’s actually just a button. You can also click it normally to perform some operations.
Then, you can also drag another ButtonItem to it:
Two ButtonItems:
Then, let me change the title of the thing on the left to be the same as our button:
In it, you either specify text or pictures, but you cannot specify both at the same time.
I changed it to "Cancel"
Over here, I changed it to "Done"
I drag another BarButtonItem,
As for this BarButtonItem, place it between them:
Then, this thing, we have in this text box:
After clicking, are Cancel and Confirm at both ends?
Then everyone said, how to put this?
Do I need to put it here or there?
I can't delay this "completion":
If it can't be dragged away, it will go back on its own:
However, you can select the BarButtonItem in the middle. Then, there are some system types that you should have seen before.