We often start with Helloworld when learning a development language, so I started my iphone development journey with Helloworld and learned about the architecture and lifecycle of iphone applications.
Open xcode to create a UIView-based project. The project name is Helloworld. Open the ViewController. xib file. We need a Labe control, press and hold the mouse to open a Label, double-click the Label, and enter Helloworld. Well, it's so simple. Now we can compile and run it. If there is no accident, we will see the following results.
Easy! This is not our purpose. To develop an excellent iphone application, we need to understand the iphone application architecture and lifecycle.
All iPhone applications are built based on the UIKit framework, so they essentially have the same core architecture. UIKit is responsible for running applications and coordinating key objects required for user input and screen display. The difference between applications lies in how to configure Default objects and how to add user interfaces and behaviors through custom objects.
From application startup to exit, the UIKit framework manages most of the critical infrastructure. IPhone applications constantly receive events from the system and must respond to those events. Receive event isUIApplication
But you need to customize the code to respond to events. To understand where event responses are needed, we need to understand the entire lifecycle and event cycle of the iPhone application. The following sections describe these cycles and summarize some key design patterns used in iPhone application development.
Application Lifecycle
The lifecycle of an application is composed of a series of events that occur between the startup and termination of the application. In iPhone OS, you can tap the icon on the Home screen to start the application. Shortly after clicking the icon, the system displays a transitional image and calls the correspondingmain
Function to start the application. After this point, a lot of initialization work will be handed over to the UIKit, which loadsUser InterfaceAnd prepare the event loop. During the event loop process, UIKit will distribute the event to yourCustom objectAnd respond to the commands issued by the application. When you exit the application, UIKit notifies the application and starts the application termination process.
Figure 1-1 shows a simplified iPhone application lifecycle. This block diagram shows the sequence of events that occur from application startup to exit. When the application is initialized and terminated, UIKit sendsDelegateThe object sends a specific message to let it know what is happening. In the event loop, UIKit distributes the event to the Custom Event processor of the application. Information on how to handle initialization and termination events will be discussed in the subsequent "initialization and termination" section. The process of event processing is described in the "event processing cycle" section, further details will be discussed in later chapters.
Figure 1-1Application Lifecycle
Note: The above content comes from Apple's official instructions
Through the above instructions, we have a general understanding of the composition and lifecycle of the iphone application. Next we will extend our Helloworld.
Drag two Button controls and double-click them to change them to Show and Clear.
Open the ViewController. h file and modify it as follows:
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (nonatomic, retain) IBOutlet UITextView *text;- (IBAction)showText:(id)sender;- (IBAction)clearText:(id)sender;@end
Open the ViewController. m file and modify it as follows:
//// ViewController.m// HelloWorld//// Created by YQ-010 on 4/12/12.// Copyright (c) 2012 __MyCompanyName__. All rights reserved.//#import "ViewController.h"@implementation ViewController@synthesize text;- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use.}- (IBAction)showText:(id)sender{ NSString *hello = [[NSString alloc] initWithFormat:@"Hello,world!"]; self.text.text = hello; [hello release];}- (IBAction)clearText:(id)sender{ NSString *nullText = [[NSString alloc] initWithFormat:@""]; self.text.text = nullText; [nullText release];}#pragma mark - View lifecycle- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.}- (void)viewDidUnload{ self.text = nil; [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil;}- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];}- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated];}- (void)viewWillDisappear:(BOOL)animated{[super viewWillDisappear:animated];}- (void)viewDidDisappear:(BOOL)animated{[super viewDidDisappear:animated];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ // Return YES for supported orientations if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } else { return YES; }}- (void) dealloc{ [text release]; [super dealloc];}@end
We provide a response for each of the two buttons. When we press the button, the showText and clearText functions we set will be executed. Do not forget to connect the button to our File's Owner. Compile and run:
After interaction, is it different? We may be confused about how to handle button events. Don't worry, we will learn it next time.
This is the case. If you have any questions, please leave a message so that you can learn and communicate with each other!
Note: Please indicate the source for reprinting!