启动界面:
开发环境版本:
模拟器运行效果:
main.m
// // main.m // 01-加法计算器 // // Created by ChenQianPing on 16/1/20. // Copyright © 2016年 chenqp. All rights reserved. // #import <UIKit/UIKit.h> #import "AppDelegate.h" // IOS程序是从main开始运行的 int main(int argc, char * argv[]) { @autoreleasepool { // 这个方法内部是一个消息循环(相当于一个死循环),因此运行到这个方法UIApplicationMain之后程序不会自动退出,而只有当用户手动关闭程序这个循环才结束。 // 这个方法有四个参数: // 第一个参数和第二个参数其实就是main函数的参数,分别代表:参数个数、参数内容; // 第三个参数代表UIApplication类(或子类)字符串,这个参数默认为nil则代表默认为UIApplication类,用户可以自定义一个类继承于这个类;如果为nil则等价于NSStringFromClass([UIApplication class]),大家可以自己试验,效果完全一样;UIApplication是单例模式,一个应用程序只有一个UIApplication对象或子对象; // 第四个参数是UIApplication的代理类字符串,默认生成的是AppDelegate类,这个类主要用于监听整个应用程序生命周期的各个事件(其实类似于之前我们文章中提到的事件监听代理),当UIApplication运行过程中引发了某个事件之后会调用代理中对应的方法。该类继承自UIResponder,这是使用storybaord的要求,而以前使用使用nib时,应用委托类是直接继承自NSObject的。而且必须有一个不是UIOutlet类的Window属性声明才可以; // 当执行UIApplicationMain方法后这个方法会根据第三个参数创建对应的UIApplication对象,这个对象会根据第四个参数AppDelegate创建并指定此对象为UIApplication的代理;同时UIApplication会开启一个消息循环不断监听应用程序的各个活动,当应用程序生命周期发生改变UIApplication就会调用代理对应的方法。 return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } }
AppDelegate.h
// // AppDelegate.h // 01-加法计算器 // // Created by ChenQianPing on 16/1/20. // Copyright © 2016年 chenqp. All rights reserved. // #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
AppDelegate.m
// // AppDelegate.m // 01-加法计算器 // // Created by ChenQianPing on 16/1/20. // Copyright © 2016年 chenqp. All rights reserved. // #import "AppDelegate.h" @interface AppDelegate () @end // AppDelegate类就是整个应用的代理类,它实现了UIAppliactionDelegate协议,它里有好几个监听应用程序的方法。 //(delegate可处理的事件包括:应用程序的生命周期事件(如应用的开启和关闭),系统事件(如来电),内存警告等)。 @implementation AppDelegate // 当应用程序启动完毕的时候就会调用(系统自动调用) - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. NSLog(@"应用将要启动"); 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(@"应用屏幕将要失去屏幕焦点"); } // 应用程序进入后台的时候调用 - (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(@"应用已经进入后台"); } // 应用程序即将进入前台的时候调用 - (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(@"应用将要进入前台"); } // 重新获取焦点(能够和用户交互) - (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(@"应用已经获得屏幕焦点"); } // 应用程序即将被销毁的时候会调用该方法 - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. NSLog(@"应用将要终止"); } @end
ViewController.h
// // ViewController.h // 01-加法计算器 // // Created by ChenQianPing on 16/1/20. // Copyright © 2016年 chenqp. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController //在类扩展中,声明3个属性,用来访问storyboard中的3个控件 //IBOutlet和weak的作用会在后面解释 @property (nonatomic,weak) IBOutlet UITextField *number1; @property (nonatomic,weak) IBOutlet UITextField *number2; @property (nonatomic,weak) IBOutlet UILabel *result; //这里先把IBAction看做是void -(IBAction)compute; @end
ViewController.m
// // ViewController.m // 01-加法计算器 // // Created by ChenQianPing on 16/1/20. // Copyright © 2016年 chenqp. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)compute { //获取第一个数值 int num1 = [self.number1.text intValue]; //获取第二个数值 int num2 = [self.number2.text intValue]; //设置文本标签的值 self.result.text = [NSString stringWithFormat:@"%d",num1 + num2]; NSString *number1Text = _number1.text; NSString *number2Text = _number2.text; NSLog(@"num1=%@,num2=%@",number1Text,number2Text); //暂时理解:叫出键盘的那个视图就是第一个响应者(FirstResponder) //resignFirstResponder代表这个视图不想当第一响应者了,于是键盘就会退出 //[_number1 resignFirstResponder]; //[_number2 resignFirstResponder]; } @end
时间: 2024-10-06 05:38:23