UI控件6 UIViewController(1)详细解析+纯代码

 

1.UIViewController的概念
2.UIViewController的属性
3.UIViewController的基本用法
4.UIKit框架代码介绍

把info.plist文件中的 Main storyboard file base name文件删去
在AppDelegate.h文件中 重写

AppDelegate.m文件的使用:

#import "AppDelegate.h"
//导入视图控制器
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //先创建一个UIWindow对象
    //属于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];
    
    
    
    // Override point for customization after application launch.
    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 invalidate graphics rendering callbacks. 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 active 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.h文件的使用

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
//当视图控制器第一次被加载显示视图时,调用此函数
//布局初始化视图使用,初始化资源使用
- (void)viewDidLoad {
    //调用父亲类的加载视图函数
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIView * view = [[UIView alloc]init];
    
    view.frame = CGRectMake(100, 100, 100, 200);
    
    //将视图添加到当前控制视图上
    [self.view addSubview:view];
    
    view.backgroundColor = [UIColor blueColor];
    
    self.view.backgroundColor = [UIColor orangeColor];
}

//当系统内存过低时,会发起警告信息,调用此函数
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    NSLog(@"内存过低!");
}


@end

main.m文件的实现:

//整个程序APP的入口函数
int main(int argc, char * argv[]) {
    @autoreleasepool {
        //UIKit 框架结构的启动函数
        //参数1:argc: 启动时带有参数的个数
        //参数2:argv:  参数列表
        //参数3:要求传入一个主框架类类名,如果参数为nil 系统会自动用默认的框架类作为主框架类
        //参数4:主框架的代理类对象
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
展开阅读全文

没有更多推荐了,返回首页