Analysis code for "Hello World" in iPhone Development

Source: Internet
Author: User

The first program developed by each learning program is "Hello World". As a developer of the iPhone application just getting started, it is very important to master the analysis code of "Hello World. This article describes the analysis code of the Hello World Program, that is, how the program says Hello.

BKJIA recommendation topic: iPhone application development

The basic running sequence of this program is: loading window (UIWindow)-> loading custom interface (MyViewController), and processing of various messages is in the Custom interface. the design of the program follows the MVC (Model-View-Controller) method, that is, the interface and the program are separated and connected to each other through the controller.


IPhone Hello World Program

First, check the window. There are two lines in the HelloWorldAppDelegate. h file:

 
 
  1. IBOutlet UIWindow *window;  
  2. MyViewController *myViewController; 

The first line defines the window of the program, and the second line defines our own interface. In the HelloWorldAppDelegate. m file, the function

-(Void) applicationDidFinishLaunching :( UIApplication *) the application is a frequently used product for iPhone developers. It defines the work to be done after the program starts. the job of these programs is to specify myViewController as a sub-interface,

 
 
  1.  
  2.  
  3. MyViewController *aViewController = [[MyViewController alloc] initWithNibName:@”HelloWorld” bundle:[NSBundle mainBundle]];  
  4. self.myViewController = aViewController;  
  5. [aViewController release];  

And display the sub-interface above.

 
 
  1.  
  2.  
  3. UIView *controllersView = [myViewController view];  
  4. [window addSubview:controllersView];  
  5. [window makeKeyAndVisible];  

As mentioned above, the program design follows the MVC method, but we haven't introduced how the code is connected with the interface. That is to say, we talked about what the program's UIWindow and view controller are doing, the interface is also drawn. How does the iPhone know which class corresponds to which interface? This is done in IB (Interface Builder). Double-click HelloWorld. xib to open IB. The following shows our Interface.

IPhone interface

Point to File's Owner. In Identity Viewer, note that the Class is MyViewController, which defines the correspondence between Model and View. in the same xib File, the File's Owner is set to a class and points to its View. The corresponding relationship is established.

 

In the MyViewController. m file,

 
 
  1.  
  2.  
  3. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  
  4. {  
  5. // Dismiss the keyboard when the view outside the text field is touched.  
  6. [textField resignFirstResponder];  
  7. // Revert the text field to the previous value.  
  8. textField.text = self.string;  
  9. [super touchesBegan:touches withEvent:event];  
  10. }  

The function is to respond to the touch. When the touch is outside the keyboard, the keyboard is revoked through resignFirstResponder.

 
 
  1.  
  2.  
  3. - (BOOL)textFieldShouldReturn:(UITextField *)theTextField {  
  4. // When the user presses return, take focus away from the text field so that the keyboard is dismissed.  
  5. if (theTextField == textField) {  
  6. [textField resignFirstResponder];  
  7. // Invoke the method that changes the greeting.  
  8. [self updateString];  
  9. }  
  10. return YES;  
  11. }  

The function is to hide the keyboard after entering the text and pressing Return, and call the updateString command to update the display. The command is as follows:

 
 
  1.  
  2.  
  3. - (void)updateString {  
  4. // Store the text of the text field in the ‘string’ instance variable.  
  5. self.string = textField.text;  
  6. // Set the text of the label to the value of the ‘string’ instance variable.  
  7. label.text = self.string;  
  8. }  

Simply put, the original text of the tag is replaced with the input text to update the display.

Now, we will introduce the analysis code of Hello World. The functions of the main statements are explained. I hope you will like it.

This article is from the blog:Http://lichen1985.com/blog/

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.