IOS Simple View startup process, iossimple

Source: Internet
Author: User

IOS Simple View startup process, iossimple
Create a project

Select | File | New | Project. Select Simple View Application in the pop-up menu to create a New Project. T1

AppDelegate. h,

  • AppDelegate. m
  • ViewController. h
  • ViewController. m
  • Main. stroyboard
  • Info. plist
  • Main. m and other files
  • Start Process

    The app startup process is as follows:

    Main Function
    # Import <UIKit/UIKit. h> // UIKit is a Cocoa Touch-based framework that includes UILable, UIButton, and other control elements # import "AppDelegate. h "// declares the AppDelegate function, used to complete the Initialization Configuration of the App and other int main (int argc, char * argv []) {@ autoreleasepool {NSLog (@" main "); // call AppDelegate
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));  
        }}

    Like C code, main is the entry function. In the main function, <UIKit/UIKit. h> and AppDelegate. h are imported.

    <UIKit/UIKit. h> is the main iOS framework and contains various main controls.

    The parameters of the main function are the same as those of other standard Linux main functions. int argc indicates the number of parameters. char * argv [] indicates the following parameters. Each parameter is separated by a space.

    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

    The UIApplicationMain () method creates an instance of the UIApplication according to the AppDelegate class name we provide, and
    AppDelegate is used as the delegate of the UIApplication. Generally, we can use the class method [UIApplication delegate application] to obtain a reference to the UIApplication.

    When UIApplication receives system events and lifecycle events, it passes the corresponding events to UIApplicationDelegate for processing. The lifecycle functions listed in the following table are mostly optional.

    AppDelegate

    AppDelegate is a proxy for the entire application. It provides interfaces similar to monitoring such as program startup and exit.

    AppDelegate. h
    # Import <UIKit/UIKit. h> // The method to implement UIApplicationDelegate @ interface AppDelegate: UIResponder <UIApplicationDelegate> // declares the UIWindow class, which is named the window attribute and is a window, used to receive messages and load controls @ property (strong, nonatomic) UIWindow * window; @ end

    In the AppDelegate class, the UIResponder class is inherited and the UISpplicationDelegate protocol must be implemented.

    AppDelegate. m

    When UIApplication receives system events and lifecycle events, it passes the corresponding events to UIApplicationDelegate for processing,

    #import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    NSLog(@"didFinishLaunchingWithOptions");    return YES;}- (void)applicationWillResignActive:(UIApplication *)application {    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.    NSLog(@"applicationWillResignActive");}- (void)applicationDidEnterBackground:(UIApplication *)application {    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.    NSLog(@"applicationDidEnterBackground");}- (void)applicationWillEnterForeground:(UIApplication *)application {    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.    NSLog(@"applicationWillEnterForeground");}- (void)applicationDidBecomeActive:(UIApplication *)application {    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.    NSLog(@"applicationDidBecomeActive");}- (void)applicationWillTerminate:(UIApplication *)application {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.    NSLog(@"applicationWillTerminate");}@end

    For details, see the following table.

    Function Description
    -(Void) applicationWillResignActive:( UIApplication *) application When the application is about to run in an inactive state, during this period, the application does not receive messages or events, such as calls
    -(Void) applicationDidBecomeActive:( UIApplication *) application When the application is executed in the active state, this is exactly the opposite of the method above.
    -(Void) applicationDidEnterBackground:( UIApplication *) application Called when the program is pushed to the background. So to set the background to continue running, you can set it in this function.
    -(Void) applicationWillEnterForeground:( UIApplication *) application It is called when the program is about to return to the foreground from the background, which is just the opposite of the above method.
    -(Void) applicationWillTerminate:( UIApplication *) application When the program is about to exit, it is called, usually used to save data and clean up before exiting. You need to set the key value of UIApplicationExitsOnSuspend.
    -(Void) applicationDidReceiveMemoryWarning:( UIApplication *) application The iPhone device only has limited memory. If too much memory is allocated to the application, the operating system will terminate the operation of the application. This method will be executed before the termination, you can usually clean up the memory here to prevent the program from being terminated.
    -(Void) applicationDidFinishLaunching:( UIApplication *) application Run after the program is loaded
    -(Void) application :( UIApplication) applicationWillChangeStatusBarFrame:( CGRect) newStatusBarFrame Execute when the StatusBar box is about to change

    -(Void) application :( UIApplication *) application willChangeStatusBarOrientation:
    (UIInterfaceOrientation) newStatusBarOrientation
    Duration :( NSTimeInterval) duration

    Run the following command when the orientation of the StatusBar box is about to change.
    -(BOOL) application :( UIApplication *) application handleOpenURL :( NSURL *) url Run through url

     

    Applications in the iPhone are easily disturbed. For example, an incoming call may cause the application to lose focus. If the application receives a call at this time, the application will go to the background to run.
    There are many other similar events that will cause the iPhone application to lose focus. Before the application loses focus, the applicationWillResignActive () method of the delegate class will be called,
    The applicationDidBecomeActive () method is called when the application gets the focus again.

    For example, when running an application, the screen lock will call the applicationWillResignActive () method of the delegate class, And the applicationDidBecomeActive () method will be called when the screen is unlocked.

    If the UIApplication delegates AppDelegate, AppDelegate must implement the UIApplicationDelegate protocol. This protocol can be used as an interface in java,
    The Protocol defines a series of methods. We must implement them in the subclass, and then the underlying UIApplication will automatically call the methods we have defined. This is a bit similar to the Ioc direction control mechanism in java.

    Load and configure the application example Page of AppDelegate

    In the didfinishlaunchingwitexceptions method, you can complete page initialization preparations, such as creating various views, reading and setting configuration files.

    Example of reading and configuring:

    NSUserDefaults * usrConfig = [NSUserDefaults standardUserDefaults]; // create a UserDefaults object // check whether the app has run if ([usrConfig boolForKey: @ "hasRunBefore"]! = YES) {// set a flag so that the field in this if statement is executed only at the first run [usrConfig setBool: YES forKey: @ "hasRunBefore"]; [usrConfig synchronize]; // synchronize the set key-value pairs

    Create a window and set the background

    Self. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]; // initialize window self. window. backgroundColor = [UIColor whiteColor]; // sets the background color [self. window makeKeyAndVisible]; // display window

    Set the number icon on the icon

    // Set the number icon on the main interface, which is introduced in 2.0. The default value is 0 [UIApplication sharedApplication]. applicationIconBadgeNumber = 4;

    Whether redo and undo operations are supported when you set shaking gestures

    // Shake the gesture to determine whether redo undo operations are supported. // Introduced after 3.0. The default value is YES [UIApplication sharedApplication]. applicationSupportsShakeToEdit = YES;
    Global variable settings

    You can define global variables in AppDelegate. h.

    @interface AppDelegate:UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (strong, nonatomic) NSMutableArray*info;@property (strong, nonatomic) NSMutableArray *name;@end

    In the didfinishlaunchingwitexceptions method of AppDelegate. m, initialize these attributes:

    self.info = [NSMutableArray arrayWithObjects:@"",@"",@"",nil];self.name = [NSMutableArray arrayWithObjects:@"",@"",@"",nil];

    Obtain global variables in other methods that require using global variables

    AppDelegate *appDelegate=[[UIApplication sharedApplication] delegate];
    StoryBoard

    The UIApplication object scans the Info. plist file and loads the storyboard according to the preset storyboard name.

    # Import <UIKit/UIKit. h> @ interface ViewController: UIViewController @ end

    In ViewController. m, complete the final configuration and loading of the interface.

    #import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.     NSLog(@"viewDidLoad");}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

    Now, you can load and configure SimpleViewApplication.

    The proofing process is as follows:

    When the system enters the background, WillResignActive and DidEnterBackground are called.

    Copyright, reprinted with the author name

    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.