IOS Apprentice Chinese version – start with iOS development from 0 – fifteenth

Have a deep processing

Before you write code to insert text into a list as a new record, let’s first improve the way we work and design the new to-do list.

For example: if you can automatically pop-up keyboard for the user, rather than the keyboard when the user clicks on the keyboard, so that the experience will be better.

To achieve this, you need to add a method viewWillAppear () to the AddItemViewController.swift:

Override func viewWillAppear (_ animated: Bool) {super.viewWillAppear} (animated) (textField.becomeFirstResponder)

The view controller receives messages from viewWillAppear () before it is about to be visualized, which is the perfect time to activate the text box. We send becomeFirstResponder () messages here.

If you do this on other platforms, it is often referred to as “control focus””. In the iOS terminology, this control method is called the first response (first responder)”

Run app and then click on the button, you can see the keyboard will slide out.

(again, if the keyboard on the emulator doesn’t automatically slip out, you can wake up the keyboard via the command+K key combination)

Small details like this usually bring good user experience. Compared to having to click text boxes to type, it’s much quicker now. In this age of change, people don’t have much patience when using products. For example, a little flaw can cause the user to transfer to your competitor’s product. I always spend a lot of energy trying to make my app as perfect as possible.

So, let’s move on to the input box.

Open the story template and select the text box, go to the property inspector and set it in the following list:

1, Placeholder:Name, of, the, Item

2, Font:System 17

3, Adjust, to, Fit:Uncheck, this

4, Capitalization:Sentence

5, Return Key:Done

IOS Apprentice Chinese version - start with iOS development from 0 - fifteenth
sets the properties of the text box

Here are some options to configure the properties of the keyboard when the text box is activated.

For example, if the text box can only enter values, you can set Keyboard Type to Number Pad. If this is a text box for entering the Email address, you can use Keyboard Type as E-mail Address. For our app, the default type is just symbolic, because users can freely enter what they’re going to do.

You can also change the name of the carriage return key on the keyboard. The default is “return”, where we change to “Done””. This is a button on the title text, automatically close the current interface click on the “Done” is not, you will also need the “Done” button on the keyboard pointing to the same action, such as the Done button on the keyboard to the Done button on the navigation bar and play the same role.

Select text field, then open the link checker, drag the Did End on Exit event to the view controller, and select the done action.

You can also drag and drop directly to the source code’s done () method if your auxiliary editor is not in use.

IOS Apprentice Chinese version - start with iOS development from 0 - fifteenth
link text field to done () action method

To view the link to the done action, click the small circle on the left edge of the done () method, and then click on a window to display the link to the done () method.

When you run app, click the Done button on the keyboard, then the interface will automatically turn off and print our preset text information in the debug area.

IOS Apprentice Chinese version - start with iOS development from 0 - fifteenth
now the Done button on the keyboard works in conjunction with the Done button on the navigation bar

Validate user input and ensure that they are received, is an indispensable step. For example, the user instantly ordered the Done button, but nothing entered. What would happen?.

In the list of main interfaces, adding a blank line with nothing at all is not good at all. In order to avoid this, you need to prohibit the Done button until the user has entered anything.

Of course, you need to process two done buttons at the same time, one on the keyboard, one on the navigation bar. Let’s start with the Done button on the keyboard, which is a little simpler.

Select the text box, open the property inspector, and select Auto-enable Return Key.

So, now that you run app and do nothing, click on the Done button on the keyboard won’t work, try it!

IOS Apprentice Chinese version - start with iOS development from 0 - fifteenth
selects Auto-enable Return Key

For the Done button on the navigation bar, the work is a bit more complicated. You need to listen every time the user clicks on the keyboard. Is it empty in the text box?. If it is empty, disable the Done button.

If the user itself is a bit wrong, you can exit the interface through the Cancel button, but when the user clicks the Done button, you must ensure that there is input in the text box.

In the text box, there are two kinds of input method, keyboard input and copy paste, you need to view controller a delegate for the text box.

When a text box sends an event to this delegate, you can see the text box. This delegate exists in AdditemViewController and can respond to text box events and make appropriate moves.

A view controller allows you to delegate multiple objects. AdditemViewController already has a delegate and a data source for UITableView, because it’s essentially a UITableViewController. Now it will add a delegate to the object of text field, UITextfiled.

These are two different types of delegate and you want to make the view control execute them at the same time. For more on the Commission, we’ll explain it in the next class.

How to define a delegate
in the SDK of the iOS system, where delegates are everywhere, so we’d better remember that it’s always done in three steps.
1, you declare that you have the power to be a delegate – to be a delegate for UITextField, and you need to write the UITextFieldDelegate to the class of the view controller. This tells the compiler that the view controller can process messages from the text box.
2, you let that problematic object know that the view controller is willing to be its delegate — in our example, the object is UITextField. If you forget to tell the text box that it has a delegate, the text box will never send any messages.
3, execute the delegate method – if you don’t respond to the messages you send, then the delegate won’t work at all.
generally, the delegate method is optional, so you don’t need to perform all the delegate methods. For example: UITextFieldDelegate actually declares seven methods, but you only care about textField (shouldChangeCharactersIn, replacementString).

Open AddItemViewController.swift and add UITextFieldDelegate to the declaration of the class.

Class, AddItemViewController:, UITableViewController, UITextFieldDelegate

Now the view controller is like saying, “I can do the delegate for the text filed object”,
, and you also need to let text field know the existence of this delegate.

Open the story template and select text field.

There are several ways to link text field’s delegate outlet to the view controller. I prefer to drag and drop the delegate from the link checker to the view controller (yellow icon, see below):

IOS Apprentice Chinese version - start with iOS development from 0 - fifteenth
drag the delegate from the text box from the link indicator to the view controller

You also need to add a outlet to the Done button on the navigation bar, so you can send messages to the inside of the view controller to disable it.

Open the auxiliary editor and make sure the auxiliary editing interface shows AddItemViewController.swift.

Select the Done button of the navigation bar, and press Ctrl to drag it to the swift file in the secondary edit window, and name the new outlet doneBarButton.

This is the swift file. This line should be added:

@IBOutlet, weak, VaR, doneBarButton:, UIBarButtonItem!

Add the following method at the bottom of the AddItemViewController.swift:

Func textField (textField: UITextField shouldChangeCharactersIn _, range: NSRange, replacementString string: String -> Bool) {let oldText = textField.text as NSString let newText! = oldText.replacingCharacters (in: range, with: string) as NSString if newText.length > doneBarButton.isEnabled true = {0} else {doneBarButton.isEnabled}} = false return true

This is one of the UITextField’s delegate methods. Every time the user to modify the text after it will be called either input or copy paste.

First, you determine the content of the new text:

Let, oldText = textField.text, as, NSString, let, newText = oldText.replacingCharacters (in:, range, with:, string) as NSString

The delegate method textField (shouldChangeCharactersIn, replacementString) cannot give direct content about the new text, only when the text changes.

You need to read the contents of the text box to calculate what the new text is, and replace yourself with it. This way you can get a new string object and store it in the newText constant.

The NSString and String
text strings in swift have data types of String. But in the above method, you use something called NSString. What’s the difference between these two?
NSString is the object for storing text in the Object-C language. In fact, it’s more powerful and easier than the String in Swift.
, however, Swift has a unique trick: String and NSString are bridged, which means you can use NSString instead of String. Here, you want to use the method replacingCharacters (in:with:) that belongs to NSString, so you have to make the swift know that the text is of type NSString, not String. The role of the
keyword as NSString is to notify swift that this oldText is a constant of type NSString. If you don’t do so, swift will assume that oldText is a String object based on the type, so you can’t use the replacingCharacters (in:with:) method.
says, “String isn’t the only type that has bridged Object-C, and another example is the array (Array) that can be bridged to Object-C’s NSArray.”. Because iOS’s architecture wasn’t written entirely by swift, the retirement time for Object-C was delayed.

When you’ve read the new text, you can disable or enable the Done button by calculating its length, checking if it is empty, and disabling it:

If newText.length > 0 {doneBarButton.isEnabled = true} else {doneBarButton.isEnabled = false}

Run app and knock some content in the text box, and then delete the contents, and the Done button is disabled.

The only problem is that the Done button is available when you haven’t entered anything at first. This is equivalent to what we have done before, and this is not allowed, so let’s fix this problem together.

Open the story template, select the Done button, and open the property inspector to deselect the Enabled checkbox.

From the start, the Done button is disabled.

IOS Apprentice Chinese version - start with iOS development from 0 - fifteenth
cannot click the Done button when nothing is entered

Finally, let’s optimize the code:

Replace the if statement with the following line

DoneBarButton.isEnabled = (newText.length, > 0)

The previous if statement is like this:

If newText.length > 0 {/ / length of more than 0 of the else} {/ / length is less than or equal to 0.}

You check the condition newText.length > 0 to determine whether to disable the Done button, if greater than 0 enabled, or else disable.

Note that the translation of the if statement is true: if the condition is true, then isEnable is true, and if the condition is false, then isEnable is false. In other words, isEnable is either true or false. In this way, it can be reduced to:

DoneBarButton. isEnable = the, result, of, the, condition

That’s what we just saw:

DoneBarButton.isEnabled = (newText.length, > 0)

In fact, the parentheses here are not necessary because there is only one operation, so there is no priority. We can simplify it even more:

DoneBarButton.isEnabled = newText.length > 0

However, doing so can make readability worse, so I’m always used to add parentheses.

By the way, let’s briefly introduce the comparison operators in Swift:

More than >
<
&lt is greater than or equal to less than
= = = = equal; less than or equal to
= not equal to
!

Remember this trick, whenever you see the structure:

If, some, condition, {something = true,} else, {something = false}

Can be replaced with:

Something = (some, condition)

In the actual operation, you can use that version. I prefer short, professional people like it. What you need to remember is that when the comparison operator always returns true and false, you can express it in a short way.