Add Action and Outlet
Up to now, we have used File 'sowner to add Action and Outlet in InterfaceBuilder, and WriteClassFiles to generate application files. However, if you add an Action or Outlet on the way, the original Action will be overwritten, and we have to generate it again with File 'sowner.
Here, let's see how to add Action and Outlet without InterfaceBuilder.
Test Item
We will first create a test project UITest, which uses File 'sowner to add an Action and an Outlet.
Use File 'sowner to generate Action and Outlet
File 'sowner Under the selected state, click "WriteClassFiles..." in the menu of InterfaceBuilder, and select Replace to generate the program File.
Add Outlet
Outlet is defined in the header file of ViewController. The following is the UITestViewController. h file of the template project.
Outlet and Action definitions in UITestViewController. h file
We can see the Outlet and Action definitions in the file. Similarly, under the definition of Outlet1, we add two more outlets.
Add Outlet
After the File is saved, you can use File 'sowner to check whether the File is successfully added:
Confirm the added Outlet
Add Action
In the same way, we add two actions in UITestViewController. h. They are myAction2 and myAction3.
Add Action
Save and use the File 'sowner of InterfaceBuilder to confirm.
Confirm the added Action
In addition, actions must be defined in UITestViewController. m.
Implementation of Action
IPhone keyboard
The iPhone keyboard occupies half of the screen, so the TextField control is basically placed in the upper half of the screen. If you need to input text below the screen, you need to generate another View, the upper half of which is the TextField control. When you click the control in the lower half, the View is displayed.
IPhone keyboard
How to disable the keyboard
Disable the keyboard and use "DidEndOnExit" Action. Associate it with the TextField control.
Associated with "DidEndOnExit"
In addition, set the button in the lower right corner of the keyboard to the close button. The button defaults to "return" or line feed. Here, we set "ReturnKey" to "Done" by setting the textbutes of the TextField control, which means to press this button to activate the "DidEndOnExit" Action above.
Set ReturnKey
Activate "DidEndOnExit" Action
Button for disabling the keyboard
You can set the exit button on the keyboard used above. However, in NumberPad or PhonePad, the button does not exist in the lower right corner. For example:
NumberPad
In this case, we need to customize a button to close the keyboard. As shown in, configure a button in the bottom right corner of the TextField control and associate it with the TouchDown Action.
To end the input on the Input Keyboard, use the Outlet associated with TextField to perform the following operations.
[OutletendEditing: YES];
Link button and keyboard
The button added above is only valid when the keyboard appears, so we try to associate it with the keyboard. It appears only when the keyboard appears. Otherwise, it is not displayed. To control the button, we need to generate an Outlet to control the display and hiding of the button.
This button is valid when text is input on the TextField control. In this case, the TextField control's Action is "EditingDidBegin". In this case, the Outlet variable of the button is "button_outlet ).
[Button_outletsetHidden: NO];
Next, click the append button in the Action to hide the operation.
[Button_outletsetHidden: YES];
In addition, after the program is started, the default status of the button should also be hidden. The "Drawing" attribute of the "inspector" select button is "Hidden 」.
Transparent button
Here we will introduce a transparent button method. Close the keyboard by clicking a field other than the TextField control. This is convenient when there is no position for setting buttons. The setting is also very simple. In the inspector, select the "Type" attribute of the button as "Custom". In this way, you can set the transparent button.
Set button
If M is selected, the transparent button overwrites the upper half of the View.
With the above settings, you can use transparent buttons. However, you need to note that after setting such a button, it will overwrite other controls in its field, so that messages of other controls cannot be triggered. Therefore, the display sequence of transparent controls must be the lowest layer. Select "Layout"> "SendBackward" in the InterfaceBuilder menu, as shown in the following figure.
Set button class
Confirmation dialog box
A confirmation dialog box is displayed when you need to confirm the operation. UIActionSheet is used here. First, use the proxy of UIActionSheet and use the protocol of Objective-C to process it in UIViewController.
@ InterfaceUITestViewController: UIViewController {
Show dialog box
In myAction1 of the UITestViewController. m file, the implementation dialog box is displayed.
-(IBAction) myAction1 :( id) sender
{
UIActionSheet * actionSheet = [[UIActionSheetalloc] initWithTitle: @ "select" delegate: selfcancelButtonTitle: @ "CANCEL" destructiveButtonTitle: @ "OK" otherButtonTitles: nil];
[ActionSheetshowInView: self. view];
[ActionSheetrelease];
}
User operations
When you press a button, use the UIActionSheet interface.
-(Void) actionSheet :( UIActionSheet *) actionSheetclickedButtonAtIndex :( NSInteger) buttonIndex {
If (buttonIndex = 0 ){
// DestructiveButton is pressed
}
}
Data Storage
The input data in the TextField control automatically disappears when the application ends. What should I do if I want to recover the next Startup? Here you can use a method similar to Windows registry. Only a small amount of data is set here. We recommend that you use SQLite or Bento for big data)
Save
[[NSUserDefaultsstandardUserDefaults] setObject: myObjforKey: @ "myKey"];
MyObj is the stored data, and myKey is an arbitrary string.
Extract
[[NSUserDefaultsstandardUserDefaults] valueForKey: @ "myKey"];
The data specified by myKey is extracted and put into myObj.