IPhone development entry (9)-Application Skills in actual development

Source: Internet
Author: User

Add Action and Outlet

Up to now, we have used File's Owner in Interface Builder to add Action and Outlet, and Write Class Files 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 the File's Owner.

Here, let's see how to add Action and Outlet without using Interface Builder.

Test Item

We will first create a test project UITest, which uses the File's Owner to add an Action and an Outlet.

 

Use File's Owner to generate Action and Outlet

When the File's Owner is selected, click "Write Class Files..." in the Interface Builder menu 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 the File's Owner 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's Owner of Interface Builder 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

Use Did End On Exit Action to disable the keyboard. Associate it with the Text Field control.

 

Associated with "Did End On Exit"

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 "Return Key" to "Done" by setting Attributes of the Text Field control 」, indicates to press this button to activate the above "Did End On Exit" Action

 

Set Return Key

 

Activate "Did End On Exit" Action

Button for disabling the keyboard

You can set the exit button on the keyboard used above, but in "Number Pad" or "Phone Pad", there is no such button in the lower right corner. For example:

 

Number Pad

In this case, we need to customize a button to close the keyboard. As shown in, configure a button in the lower right corner of the Text Field control and associate it with the Touch Down Action.

 

To end the input on the Input Keyboard, use the Outlet associated with the Text Field to perform the following operations.

[outlet endEditing: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 you start to input Text on the Text Field control. In this case, the Text Field control's Action is "Editing Did Begin", in which we will display the button (the Outlet variable of the button is button_outlet ).

 

[button_outlet setHidden:NO];

Next, click the append button in the Action to hide the operation.

[Button_outlet setHidden: 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 Text Field 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. After selecting the button below, select "Layout"> "Send Backward" in the Interface Builder menu.

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.

@interface UITestViewController : UIViewController <UIActionSheetDelegate> {
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];
[ActionSheet showInView:Self. View];
[ActionSheet release];
}

 

User operations

When you press a button, use the UIActionSheet interface.

 

-(Void) ActionSheet :(UIActionSheet*)ActionSheetClickedButtonAtIndex :(NSInteger)ButtonIndex{If(ButtonIndex = 0) {// destructiveButton is pressed} save data

The data input in the Text Field 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)

[[NSUserDefaults standardUserDefaults] setObject:myObj forKey:@"myKey"];
Save

MyObj is the stored data, and myKey is an arbitrary string.

[[NSUserDefaults standardUserDefaults] valueForKey:@"myKey"];
Extract

The data specified by myKey is extracted and put into myObj.

Author: Yi Feiyang

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.