码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发从入门到精通--UIViewController基础

时间:2016-07-19 11:14:33      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

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基础

标签:

原文地址:http://blog.csdn.net/android_it/article/details/51932036

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!