標籤:
UIViewController基礎
我們還是不用storyboard,下面我們還是以代碼來逐一說明:
#import <UIKit/UIKit.h>#import "AppDelegate.h"//整個app程式的主函數,入口函數int main(int argc, char * argv[]) { //自動記憶體釋放池 @autoreleasepool { //UIKit架構結構的啟動函數 //參數一:argc,啟動時帶有參數的個數 //參數二:argv,參數列表 //參數三:nil,要求傳人一個主架構類類名,如果參數為nil,系統會自動用預設的架構類作為主架構類 //參數四:主架構的代理類對象名字 return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); }}
在”AppDelegate.m” 檔案裡面:
#import "AppDelegate.h"#import "ViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //建立一個window對象 //window對象屬於AppDelegate的屬性 //UIScreen:表示螢幕硬體類 //mainScreen:獲得主畫面的資訊 //bounds:當前手機螢幕的大小尺寸 self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds]; //建立視圖控制器對象 ViewController * vcRoot =[[ViewController alloc]init]; //對視窗的根視圖控制器進行賦值操作 //整個UIKit架構中只有一個根視圖控制器,屬於window的屬性 //視圖控制器用來管理介面和處理介面的邏輯類對象 //現在要求程式啟動前必須對根視圖控制器複製 self.window.rootViewController=vcRoot; //將window作為主視圖並顯示出來 [self.window makeKeyAndVisible]; 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.}- (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.}- (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.}- (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.}- (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.}@end
在ViewController.h檔案裡面:
#import <UIKit/UIKit.h>//所有的控制器都需要自訂來完成//繼承於官方的UIViewController@interface ViewController : UIViewController@end
在ViewController.m檔案裡面:
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController//當視圖控制器第一次被載入顯示視圖時,調用此函數//布局初始化視圖來使用,初始化資源- (void)viewDidLoad { //調用父類的載入視圖函數 [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //建立一個view對象 UIView * view =[[UIView alloc]init]; view.frame=CGRectMake(100, 100, 100, 200); //將視圖添加到當前控制視圖上 [self.view addSubview:view]; //設定自己定義的一個view的背景顏色 view.backgroundColor=[UIColor blueColor]; //設定主視窗的背景顏色 self.view.backgroundColor=[UIColor orangeColor];}//當系統記憶體過低時,會發起警告資訊,調用此函數- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
iOS開發從入門到精通--UIViewController基礎