iOS 控制器創建\導航控制器

iOS應用程序啟動流程

  • 1.打開程序
  • 2.執行main函數
  • 3.執行UIApplicationMain函數
  • 4.初始化UIApplication(創建和設置代理對象,開啟事件循環)
  • 5.監聽系統事件
    • 1.程序加載完畢
     application:didFinishLaunchingWithOptions:
    
    • 2.程序獲得焦點
    applicationDidBecomeActive:
    
    • 3.程序進入后臺
    applicationDidEnterBackground:
    
    • 4.程序失去焦點
    applicationWillResignActivie:   
    
    • 5.程序從后臺回到前臺
    applicationWillEnterForeground:
    
    • 6.內存警告,可能要終止程序
    applicationDidReceiveMemoryWarning:
    
    • 7.程序即將推出
    applicationWillTerminate:
    
程序啟動完成的時候調用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    NSLog(@"%s",__func__);
    return YES;
}
當app失去焦點的時候調用
- (void)applicationWillResignActive:(UIApplication *)application {
        NSLog(@"%s",__func__);
    // 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.
}
app進入后臺的時候調用,app忽然打斷的時候,在這里保存一些需要用到的數據
- (void)applicationDidEnterBackground:(UIApplication *)application {
        NSLog(@"%s",__func__);
    // 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.
}
app進入即將前臺
- (void)applicationWillEnterForeground:(UIApplication *)application {
        NSLog(@"%s",__func__);
    // 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.
}
當app獲取到焦點的時候調用,意味著app可以與用戶交互
- (void)applicationDidBecomeActive:(UIApplication *)application {
        NSLog(@"%s",__func__);
    // 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.
}
app被關閉的時候調用
- (void)applicationWillTerminate:(UIApplication *)application {
        NSLog(@"%s",__func__);
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
app接收到內存警告的時候調用 用途 清空圖片的緩存
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
    NSLog(@"%s",__func__);
}

Window 的層級關系

 UIWindowLevelNormal < UIWindowLevelStatusBar < UIWindowLevelAlert

makeKeyAndVisible作用 : 成為app的主窗口并且顯示

[self.window makeKeyAndVisible];
  • 整個app中只有一個UIApplication
  • UIApplication一般用來做一些應用級別的操作(app的提醒框,聯網狀態,打電話,打開網頁,控制狀態欄)
 // 設置appIcon提醒數字,必須注冊用戶通知
   app.applicationIconBadgeNumber = 10;
   // 創建用戶通知
   UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
   // 注冊用戶的通知
   [app registerUserNotificationSettings:settings];
   
   // 設置聯網狀態
   app.networkActivityIndicatorVisible = YES;
  • 控制器控制導航欄
#pragma mark - 控制器設置狀態欄
 //在iOS7以后,狀態欄默認由控制器決定
 //隱藏狀態欄
- (BOOL)prefersStatusBarHidden
{
    return YES;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}
  • 自己創建窗口
// 程序啟動完成的時候
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 1.創建窗口,注意窗口必須要有尺寸,尺寸跟屏幕一樣大的尺寸,窗口不要被釋放
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor redColor];
    
    // 2.創建窗口的跟控制器
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor yellowColor];
    
    [vc.view addSubview:[UIButton buttonWithType:UIButtonTypeContactAdd]];
    
    // 如果設置窗口的跟控制器,默認就會把控制器的view添加到窗口上
    // 設置窗口的跟控制器,默認就有旋轉功能
    self.window.rootViewController = vc;
    
//    [self.window addSubview:vc.view];
    
    // 3.顯示窗口
    [self.window makeKeyAndVisible];
    
    return YES;
}

通過storyboard創建控制器的步驟:

  • 1、加載storyboard
  • 2、實例化控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 創建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    
    // 創建窗口的跟控制器
    // 加載storyboard
    // storyboard文件名,不需要帶后綴
    // nil:  [NSBundle mainBundle]
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    // 通過storyboard創建控制器
    // instantiateInitialViewController:加載箭頭指向的控制器
    UIViewController *vc = [storyboard instantiateInitialViewController];
    
    UIViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:@"green"];
//
    NSLog(@"%@",[vc class]);
    self.window.rootViewController = vc;
    
    // 顯示窗口
    [self.window makeKeyAndVisible];

    return YES;
}

通過 Xib 創建控制器的步驟

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    // 創建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    
    // 通過xib創建控制器
    ViewController *vc = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    
    
    self.window.rootViewController = vc;
    
    
    [self.window makeKeyAndVisible];
    
    
    return YES;
}

loadView 作用

// loadView作用:自定義控制器的view
// loadView什么時候調用:第一次使用控制器的view的時候調用
// 注意:在這個方法中如果沒有自定義view,就不能獲取控制器的view
// 一旦重寫了這個方法,就不要調用[super loadView]
// 如果重寫了這個方法,就不會去加載storyboard描述的控制器的View
- (void)loadView
{
    // 創建控制器view
    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view.backgroundColor = [UIColor purpleColor];
}

loadView直接不實現就是系統默認的做法

- (void)loadView
{
    // super ->  UIViewController
    // 系統默認的做法,一定不要這樣寫
    [super loadView];
}

控制器 View 的創建(Xib)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    
    // 加載跟類名相同的xib
    // 如果描述控制器View的xib跟控制器的類名相同,就會去加載
    // 只有控制器的init方法底層會調用initWithNibName:bundle:
    // 只要通過initWithNibName:bundle:初始化控制器,并且nibName為nil,就會執行以下幾步。
    // XMGViewController類型
    // 1.尋找有沒有跟控制器類名同名但是不帶Controller的xib,如果有就會去加載(XMGView.xib)
    // 2.尋找有沒有跟控制器類名同名的xib,如果有就會去加載(XMGViewController.xib)
    // 3.如果都沒有找到,創建空的view,
    UIViewController *vc = [[ViewController alloc] initWithNibName:@"VC" bundle:nil];
    
    self.window.rootViewController = vc;
    
    [self.window makeKeyAndVisible];
    
    return YES;
}

控制器 View 的懶加載


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 創建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 創建UIViewController控制器,控制器的view并沒有創建
    // 控制器的view懶加載:第一次使用的時候才會去加載,并不是創建UIViewController控制器的時候去加載
    UIViewController *vc = [[ViewController alloc] init];
    vc.view.backgroundColor = [UIColor redColor];
    
    self.window.rootViewController = vc;
    
    // 顯示窗口
    [self.window makeKeyAndVisible];
    
    
    return YES;
}

- (void)loadView
{
    // 如果控制器是窗口的根控制器就可以不用設置尺寸
    self.view = [[UIView alloc] initWithFrame:CGRectZero];
    
    self.view.backgroundColor = [UIColor yellowColor];
    
}
/**
 *  底層實現原理
 */
- (UIView *)view
{
    if (_view == nil) {
        [self loadView];
        [self viewDidLoad];
    }
    return _view;
}
// 在viewDidLoad中打印控制器的尺寸不準確,通常在viewDidAppear
// 控制器的view加載完成的時候調用
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.view.backgroundColor = [UIColor blueColor];
    
    
    NSLog(@"%s",__func__);
}

// view完全顯示的時候調用
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSLog(@"%@",NSStringFromCGRect(self.view.bounds));
    
}

導航控制器的基本使用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 創建窗口
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 創建導航控制器的跟控制器,也屬于導航控制器的子控制器
    UIViewController *vc = [[OneViewController alloc] init];
    vc.view.backgroundColor = [UIColor redColor];
    
    
    // 導航控制器也需要一個根控制器
    // 默認導航控制器把根控制器的view添加到導航控制器的view上
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:vc];
    
    NSLog(@"%@",navVc);
    // 設置窗口的跟控制器
    self.window.rootViewController = navVc;
    
    [self.window makeKeyAndVisible];
    
    
    return YES;
}
    // pop不是馬上把控制器銷毀,而是等到動畫結束再銷毀
    // 回到上一個界面
    [self.navigationController popViewControllerAnimated:YES];
    // 注意:只能返回到棧里面的控制器
    [self.navigationController popToViewController:self.navigationController.childViewControllers[0] animated:YES];
    
    // 返回的是根控制器
    [self.navigationController popToRootViewControllerAnimated:YES];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容