UIApplication、UIView、UIWindow、UIScreen、UIViewController、UINavigationController 介绍

转载请声明出处:http://blog.csdn.net/jinnchang/article/details/44954803

1、前言

我们先来看一下这几个概念的类继承关系图:

iOS 中,所有显示在界面上的对象都是从 UIResponder 直接或间接继承的。

2、应用程序(UIApplication)

一个 UIApplication 对象就代表一个应用程序。一个 iOS 程序启动后创建的第一个对象就是 UIApplication 对象(只有一个),而且是单例的。如有需要,可以通过如下代码获取该单例对象:

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. let app = UIApplication.sharedApplication()  
利用 UIApplication 对象,可以进行一些应用级别的操作。
(1)设置应用图标右上角的红色提醒数字

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. app.applicationIconBadgeNumber = 3  
(2)设置联网指示器的可见性
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. app.networkActivityIndicatorVisible = true  
(3)管理状态栏
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. app.setStatusBarStyle(UIStatusBarStyle.Default, animatedtrue)  
  2. app.setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)  
(4)openURL 方法
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 发信息  
  2. app.openURL(NSURL(string: "sms://10010")!)  
  3.   
  4. // 发邮件  
  5. app.openURL(NSURL(string: "mailto://jinnchang@126.com")!)  
  6.   
  7. // 打开一个网页  
  8. app.openURL(NSURL(string: "http://blog.csdn.net/jinnchang")!)  
  9.   
  10. // 跳转到 AppStore  
  11. app.openURL(NSURL(string: "https://itunes.apple.com/cn/app/qq/id444934666?mt=8")!)  
Github 上关于 UIApplication 的示例:UIApplicationSample

3、视图(UIView)

UIView 的实例即视图,负责在屏幕上定义一个矩形区域,同时处理该区域的绘制和触屏事件。视图可以嵌套,也可以像图层一样进行叠加。

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  ViewController.swift  
  3. //  UIViewControllerSample  
  4. //  
  5. //  Created by jinnchang on 15/4/8.  
  6. //  Copyright (c) 2015年 Jinn Chang. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11. class ViewController: UIViewController {  
  12.   
  13.     override func viewDidLoad() {  
  14.         super.viewDidLoad()  
  15.         // Do any additional setup after loading the view, typically from a nib.  
  16.           
  17.         // 添加一个黑色 view  
  18.         let view = UIView(frame: CGRectMake(4040self.view.frame.size.width - 80self.view.frame.size.height - 80))  
  19.         view.backgroundColor = UIColor.blackColor()  
  20.           
  21.         self.view.addSubview(view)  
  22.     }  
  23.   
  24.     override func didReceiveMemoryWarning() {  
  25.         super.didReceiveMemoryWarning()  
  26.         // Dispose of any resources that can be recreated.  
  27.     }  
  28.   
  29. }  

4、窗口(UIWindow)

UIWindow 管理和协调应用程序的显示,虽然 UIWindow 继承自 UIView,但通常不直接操作 UIWindow 对象中与视图相关的属性变量。
iOS 程序启动完毕后,创建的第一个视图控件就是 UIWindow(创建的第一个对象是 UIApplication),接着创建控制器的 view,最后将控制器的 view 添加到 window 上,于是控制器的 view 就显示在屏幕上了。一般情况下,应用程序只有一个 UIWindow 对象,即使有多个,也只有一个 UIWindow 可以接受到用户的触屏事件。

(1)自定义一个 myWindow

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. let myWindow = UIWindow(frame: UIScreen.mainScreen().bounds)  
(2)设置 myWindow 为主窗口并显示出来(view 不能凭空显示,必须依赖 window)
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. myWindow.makeKeyAndVisible()  
UIWindow 和 UIView  之间的关系也可以想象成这样一个场景:首先会有一个空的画框(UIWindow),我们在画框上放置一块画布(UIView),然后可以在这个画布上进行绘画。画布上可能会被画上各种元素,例如 UILabel、UIButton 等,这些元素其实也是一个又一个 UIView,它们会有一个层级关系管理,有点类似于 Photoshop 中图层的概念,层级高的元素会覆盖住层级低的元素,从而导致层级低的元素被部分或完全遮挡。

5、屏幕(UIScreen)

通过这个类我们可以获取一些关于屏幕的信息,通常用来获取屏幕尺寸。

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 返回带有状态栏的 Rect    
  2. let screenBounds = UIScreen.mainScreen().bounds  
  3.   
  4. // 返回不含状态栏的 Rect    
  5. let viewBounds = UIScreen.mainScreen().applicationFrame  

6、视图控制器(UIViewController)

视图控制器管理 UIView 实例的生命周期及资源的加载与释放、处理由于设备旋转导致的界面旋转、以及和用于构建复杂用户界面的高级导航对象进行交互。

下图展示了 UIView、UIWindow、UIScreen、UIViewController 之间的层级关系:


想要完整了解 UIViewController 用法可以参考 Github 上关于 UIViewController 的示例:UIViewControllerSample

7、导航控制器(UINavigationController)

在介绍 UINavigationController 之前先介绍另一个概念:容器(Container)。一个 app 可能有很多的 UIViewController 组成,这时需要一个容器来对这些 UIViewController 进行管理。大部分的容器也是一个 UIViewController,如 UINavigationController、UITabBarController,也有少数不是 UIViewController,比如 UIPopoverController 等。
UINavigationController 继承自 UIViewController,通常我们新建的工程是直接将视图控制器添加到 window 上的,如果添加了 navigation 以后就多了一层。

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  AppDelegate.swift  
  3. //  UINavigationControllerSample  
  4. //  
  5. //  Created by jinnchang on 15/4/8.  
  6. //  Copyright (c) 2015年 Jinn Chang. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11. @UIApplicationMain  
  12. class AppDelegate: UIResponder, UIApplicationDelegate {  
  13.   
  14.     var window: UIWindow?  
  15.   
  16.   
  17.     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {  
  18.         // Override point for customization after application launch.  
  19.           
  20.         // 初始化 window  
  21.         window = UIWindow(frame: UIScreen.mainScreen().bounds)  
  22.         window?.backgroundColor = UIColor.grayColor()  
  23.           
  24.         // 初始化 navigationController  
  25.         let viewController = ViewController(nibName: nil, bundle: nil)  
  26.         let navigationController = UINavigationController(rootViewController: viewController)  
  27.           
  28.         // 设置 window 的根控制器为 navigationController  
  29.         window?.rootViewController = navigationController  
  30.           
  31.         // 设置 window 为主窗口并显示出来  
  32.         window?.makeKeyAndVisible()  
  33.           
  34.         return true  
  35.     }  
  36.   
  37.     func applicationWillResignActive(application: UIApplication) {  
  38.         // 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.  
  39.         // 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.  
  40.     }  
  41.   
  42.     func applicationDidEnterBackground(application: UIApplication) {  
  43.         // 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.  
  44.         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.  
  45.     }  
  46.   
  47.     func applicationWillEnterForeground(application: UIApplication) {  
  48.         // 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.  
  49.     }  
  50.   
  51.     func applicationDidBecomeActive(application: UIApplication) {  
  52.         // 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.  
  53.     }  
  54.   
  55.     func applicationWillTerminate(application: UIApplication) {  
  56.         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.  
  57.     }  
  58.   
  59. }  
以下是 UINavigationController 的结构图:

通过 UINavigationController 进行视图切换的两种常用方式:

(1)presentViewController

[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 向上弹出视图  
  2. let firstViewController = FirstViewController(nibName: nil, bundle: nil)  
  3. self.navigationController?.presentViewController(firstViewController, animatedtrue, completion: nil)  
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 退出视图  
  2. self.dismissViewControllerAnimated(true, completion: nil)  
(2)pushViewController
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 向左推出视图  
  2. let secondViewController = SecondViewController(nibName: nil, bundle: nil)  
  3. self.navigationController?.pushViewController(secondViewController, animatedtrue);  
[objc]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. // 退出视图  
  2. self.navigationController?.popToRootViewControllerAnimated(true)  
想要完整了解 UINavigationController 用法可以参考 Github 上关于 UINavigationController 的示例:UINavigationControllerSample

8、结语

文章最后更新时间:2015年4月9日09:17:44。参考资料如下:

About View Controllers

http://www.cnblogs.com/smileEvday/archive/2012/05/14/2495153.html

http://www.cnblogs.com/wendingding/p/3766347.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值