標籤:
啟動介面:
開發環境版本:
模擬器運行效果:
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
【iOS開發之旅】第一個iOS程式