IPhoneThe application Say Hello instance Operation Section) is the content introduced in this section. Continue to the iPhone application Say Hello instance operation section). Let's start with this section.
View Controller implementation
Implementation ViewControllerYou need to do the following:
Define outlet variables and action methods and associate them with interface elements in the view of the Nib file.
Implement the related logic after clicking the button-display the corresponding call language based on the input name and determine whether the input name is empty or not.
After you click the complete Done button on the keyboard, the keyboard disappears.
Establish a connection
From the business perspective, we need to establish an association with several elements of the interface:
Text input box to get its input text
Text label to display specific text
Button to respond to its click events
Before Xcode4, Interface Builder and Xcode are separated. Generally, the socket variables and action methods are defined in Xcode, then establish the connection between the Interface element and the View Controller in InterfaceBuilder. After Xcode4 is reached, the Interface Builder and Xcode have been merged together, so there are some changes in this part, xcode4 makes this part of work easier. You can drag it directly from the view editing interface to connect to the code file.
In the SayHello project we are developing, we need to add an Action Method to the View Controller. When the button on the interface is clicked, it will send a sayHello: Message to the View Controller, therefore, create a sayHello: Action Method for the button:
In Xcode, select the Nib file RootViewController. xib corresponding to the View Controller)
Show Assistant editor
Let Assistant display the header file RootViewController. h of the View Controller)
Press and hold the Control key, and drag the button from the Nib file to the method declaration code area of the header file.
In the pop-up panel, set the connection between the button and the View Controller as Action)
Set Connection to Action
Set Name To sayHello:
Set Type to id
Set the Event to Touch Up Inside, that is, when the user clicks the button, and then triggers
Set Arguments to Sender
Click Connect to establish a connection
You have done two things by adding an action for the button.
Added the corresponding code to the class of the View Controller.
The following code is added to the header file:
- - (IBAction)sayHello:(id)sender;
In addition, corresponding implementation methods are added to the implementation file:
- - (IBAction)sayHello:(id)sender {
- }
IBAction is a special keyword. Its only function is to tell Interface Builder to treat a method as an action associated with a target or action. It is defined as void.
The connection between buttons and view controllers is established. The connection is equivalent to calling addTarget: action: forControlEvents: on the button, and the target is the File Owner File's Owner), that is, the view controller. The action is sayHello: method, the corresponding event is UIControlEventTouchUpInside.
Next, we need to establish a connection between the text input box and text Tag:
In Xcode, select the Nib file RootViewController. xib corresponding to the View Controller)
Show Assistant editor
Let Assistant display the header file RootViewController. h of the View Controller)
Press and hold the Control key to drag the text input box from the Nib file to the method declaration code area of the header file.
In the pop-up panel, set the connection between the text input box and View Controller to Outlet)
Set Connection to Outlet
Set Name To nameTextField
Set Type to UITextField
Click Connect to establish a connection
The preceding operation to add a socket variable to the text input box completes two tasks:
Added the corresponding code to the class of the View Controller.
The following code is added to the header file:
- @property (nonatomic, retain) IBOutlet UITextField *nameTextField;
In addition, corresponding implementation methods are added to the implementation file:
Added:
- @synthesize nameTextField;
Added
- [nameTextField release];
Added the following in the viewDidUnload method:
- [self setNameTextField:nil];
IBOutlet is a special keyword. It only serves to notify Interface Builder to treat an instance variable or attribute as a socket variable. In fact, this keyword is defined as blank, so it does not work during compilation.
Establishes a connection between the text input box and the View Controller. The connection is equivalent to calling the setNameTextFiled method on the View Controller. The text input box is passed in as a parameter.
Follow the above method to create the same socket variable in the text input box, create the socket variable used to display the text label of the greeting, and name the socket variable as greetingLabel with the type of UILabel.
Click the button in the view to implement the logic code. It will send the sayHello: Message to the View Controller. After that, the View Controller will obtain the text content in the text input box, update the text label used to display greetings Based on the content. The following is the sayHello in the RootViewController. m file: Implementation of the method code:
- -(IBAction) sayHello :( id) sender {
- // Obtain the content of the text input box and store it in the Variable
- NSString * nameString = nameTextField. text;
- // Check whether the input name is null. If it is null, a prompt is displayed.
- If (nameString. length = 0 ){
- UIAlertView * alertView = [[UIAlertView alloc] initWithTitle: @ "name cannot be blank" message: @ "enter a name and click the button again.
- "Delegate: nil cancelButtonTitle: @" OK "otherButtonTitles: nil, nil];
- [AlertView show];
- [AlertView release];
- GreetingLabel. text = @"";
- Return;
- }
- // Check whether the name is more than 16 characters. If the name is more than 16 characters, it is automatically truncated.
- If (nameString. length> 16 ){
- NameString = [nameString substringToIndex: 16];
- }
- // Generate a greeting based on the input name
- NSString * greeting = [NSString stringWithFormat: @ "Hello, % @! ", NameString];
- // Display greetings
- GreetinggreetingLabel. text = greeting;
- }
There are several additional instructions for this method:
UIAlertView is used to display the message prompt dialog box.
StringWithFormat: The method string creates a new string according to the format specified by the formatted string. % @ Indicates that a String object should be used here.
Hide the keyboard to compile and run the application. Enter "Jim" in the text box, click the button, and the label displays "Hello, Jim !" . However, if you select a text field for input, you will find that you have no way to indicate that the input is complete, and there is no way to eliminate the keyboard. In iPhone applications, when an element that allows text input becomes the first responder, the keyboard is automatically displayed. When the element is no longer in the first responder status, the keyboard disappears. We cannot directly send messages to the keyboard, but we can switch the first responder status of the text input element, and use the additional effect of this operation to display or remove the keyboard. In the application, when a user clicks a text field, the control becomes the first responder, so the keyboard is displayed. When you click the Done button on the keyboard, you want the keyboard to disappear.
The UITextFieldDelegate protocol contains a textFieldShouldReturn: method. Once you click Return, the text field will call this method and the title of the key is irrelevant ). However, this method can be implemented only by setting the View Controller to the Delegate of UITextField in the text input box. In the method, the resignFirstResponder message is sent to the text field, the additional effect of this message will make the keyboard disappear.
Follow these steps to set the delegate connection in the text input box:
In Xcode, select the Nib file RootViewController. xib corresponding to the View Controller)
Press and hold the Control key and click the text input box.
In the displayed translucent panel, select the dot behind the delegate and drag it to the File's Owner.
Next, use RootViewController as the delegate of the text input box nameTextField)
Add <UITextFieldDelegate> after UIViewController in the header file RootViewController. h:
@ Interface RootViewController: UIViewController <UITextFieldDelegate> {
This statement indicates that the View Controller RootViewController will support the UITextFieldDelegate protocol.
In the View Controller implementation file RootViewController. m), implement textFieldShouldReturn: method:
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- if (nameTextField == textField) {
- [nameTextField resignFirstResponder];
- }
- return YES;
- }
Because this application only has one text input box, it does not need to contain the nameTextField = textField check. However, objects may be set to multiple delegates of the same type. In this case, you need to differentiate these objects.
Now we have developed the entire application. Next we will test it.
Test
This application is relatively simple. We design several test scenarios:
Enter a normal name, such as "Baoyu", and click the button to see if "Hello, Baoyu!" is displayed !"
Do not enter any name. click the button to see if any prompt is displayed. Enter a name.
Enter 16, 17, and 20 characters respectively to see if the name can only display the first 16 characters
Click the text input box to display the keyboard. Click the Done button on the keyboard to see if the keyboard is hidden.
For this test scenario, perform functional tests one by one. The results seem to be exactly the same as we expected.
Summary: AboutIPhoneThe content of the application "Say Hello instance operation" is introduced. I hope this instance will be helpful to you. Through such a simple project, you can learn the following knowledge points: some common design patterns for iOS development;IPhoneProgram startup process; viewControllerHow to establish connections with Nib files. This knowledge is frequently used for iPhone development and iOS development.
From: http://www.cnblogs.com/dotey/archive/2011/06/09/2075954.html