iOS Development Tour First iOS program

Source: Internet
Author: User

Start interface:



Development Environment Version:


Simulator Run Effect:

Main.m

////main.m//01-Addition Calculator////Created by chenqianping on 16/1/20.//copyright©2016 year CHENQP. All rights reserved.//#import<UIKit/UIKit.h>#import "AppDelegate.h"//The iOS program is running from mainintMainintargcChar*argv[]) {@autoreleasepool {//This method internally is a message loop (equivalent to a dead loop), so the program does not automatically exit after running to this method Uiapplicationmain, and the loop does not end until the user closes the program manually. //This method has four parameters://the first parameter and the second parameter are actually the parameters of the main function, respectively: the number of parameters, the contents of the parameters;//The third parameter represents the UIApplication class (or subclass) string, which defaults to nil, which means that the default is the UIApplication class, and the user can customize a class to inherit from the class, or nil if it is equivalent to Nsstringfromclass ( [UIApplication class]), you can test their own, the effect is exactly the same; UIApplication is a singleton mode, and an application has only one UIApplication object or sub-object;//The fourth parameter is the proxy class string for UIApplication, which is generated by default for the Appdelegate class, which is primarily used to listen to events throughout the application life cycle (in fact, similar to the event listener agents mentioned in our previous article), The corresponding method in the proxy is invoked when an event is raised during the uiapplication run. This class inherits from Uiresponder, which is the requirement to use Storybaord, and the application delegate class is directly inherited from NSObject when using nib previously. And there must be a window property declaration that is not a uioutlet class;//when the Uiapplicationmain method is executed, this method creates a corresponding UIApplication object based on the third parameter, which is created and specified by the fourth parameter Appdelegate and specifies that the object is a proxy for uiapplication At the same time, UIApplication will open a message loop that listens to the various activities of the application, and when the application life cycle changes UIApplication calls the agent's corresponding method.         returnUiapplicationmain (argc, argv, Nil, nsstringfromclass ([appdelegateclass])); }}

AppDelegate.h

// //   AppDelegate.h//  01-Addition Calculator ////  Created by Chenqianping on 16/1/20. //   copyright©2016 year CHENQP. All rights reserved. // #import <UIKit/UIKit.h>@interface appdelegate:uiresponder <uiapplicationdelegate >*window; @end

Appdelegate.m

////APPDELEGATE.M//01-Addition Calculator////Created by chenqianping on 16/1/20.//copyright©2016 year CHENQP. All rights reserved.//#import "AppDelegate.h"@interfaceappdelegate ()@end//The Appdelegate class is the proxy class for the entire application, which implements the Uiappliactiondelegate protocol, which has several methods for listening to applications. //(delegate events that can be handled include: Application lifecycle events (such as application opening and closing), system events (such as incoming calls), memory warnings, etc.). @implementationappdelegate//called when the application is finished (automatically called by the system)-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {//Override point for customization after application launch.NSLog (@"app will start"); returnYES;}//called when it is about to lose its active state (loses focus, cannot interact)- (void) Applicationwillresignactive: (UIApplication *) Application {//Sent when the application are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US    Er quits the application and it begins the transition to the background state. //Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.NSLog (@"app screen will lose screen focus");}//called when the application enters the background- (void) Applicationdidenterbackground: (UIApplication *) Application {//Use the method to release the shared resources, save user data, invalidate timers, and store enough application state in    Formation to the restore your application to the it is terminated later. //If Your application supports background execution, this method is called instead of Applicationwillterminate:when th E user quits.NSLog (@"application has entered the background");}//called when the application is about to enter the foreground- (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 mad E on entering the background.NSLog (@"application will enter the foreground");}//regain focus (ability to interact with users)- (void) Applicationdidbecomeactive: (UIApplication *) Application {//Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.NSLog (@"app has got screen focus");}//This method is called when the application is about to be destroyed- (void) Applicationwillterminate: (UIApplication *) Application {//Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.NSLog (@"app will be terminated");}@end

ViewController.h

////ViewController.h//01-Addition Calculator////Created by chenqianping on 16/1/20.//copyright©2016 year CHENQP. All rights reserved.//#import<UIKit/UIKit.h>@interfaceViewcontroller:uiviewcontroller//In a class extension, declare 3 properties to access the 3 controls in storyboard//the role of Iboutlet and weak will be explained later.@property (nonatomic,weak) iboutlet Uitextfield *Number1, @property (nonatomic,weak) iboutlet Uitextfield*number2, @property (nonatomic,weak) iboutlet UILabel*result;//first of all, Ibaction is considered void .-(ibaction) compute;@end

Viewcontroller.m

////VIEWCONTROLLER.M//01-Addition Calculator////Created by chenqianping on 16/1/20.//copyright©2016 year CHENQP. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}-(void) compute{//gets the first value of the    intNUM1 =[Self.number1.text Intvalue]; //get a second value    intnum2 =[Self.number2.text Intvalue]; //set the value of a text labelSelf.result.text = [NSString stringWithFormat:@"%d", NUM1 +num2]; NSString*number1text =_number1.text; NSString*number2text =_number2.text; NSLog (@"num1=%@,num2=%@", Number1text,number2text); //temporarily understand: The view that calls out the keyboard is the first responder (FirstResponder)//Resignfirstresponder represents this view and does not want to be the first responder, so the keyboard exits//[_number1 Resignfirstresponder]; //[_number2 Resignfirstresponder];    }@end

iOS Development Tour First iOS program

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.