多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > UIApplication、UIView、UIWindow、UIScreen、UIViewController、UINavigationController 介

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

來源:程序員人生   發布時間:2015-04-25 09:44:55 閱讀次數:6279次
轉載請聲明出處:http://blog.csdn.net/jinnchang/article/details/44954803

1、前言

我們先來看1下這幾個概念的類繼承關系圖:

iOS 中,所有顯示在界面上的對象都是從 UIResponder 直接或間接繼承的。

2、利用程序(UIApplication)

1個 UIApplication 對象就代表1個利用程序。1個 iOS 程序啟動后創建的第1個對象就是 UIApplication 對象(只有1個),而且是單例的。如有需要,可以通過以下代碼獲得該單例對象:

let app = UIApplication.sharedApplication()
利用 UIApplication 對象,可以進行1些利用級別的操作。
(1)設置利用圖標右上角的紅色提示數字

app.applicationIconBadgeNumber = 3
(2)設置聯網唆使器的可見性
app.networkActivityIndicatorVisible = true
(3)管理狀態欄
app.setStatusBarStyle(UIStatusBarStyle.Default, animated: true) app.setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
(4)openURL 方法
// 發信息 app.openURL(NSURL(string: "sms://10010")!) // 發郵件 app.openURL(NSURL(string: "mailto://jinnchang@126.com")!) // 打開1個網頁 app.openURL(NSURL(string: "http://blog.csdn.net/jinnchang")!) // 跳轉到 AppStore app.openURL(NSURL(string: "https://itunes.apple.com/cn/app/qq/id444934666?mt=8")!)
Github 上關于 UIApplication 的示例:UIApplicationSample

3、視圖(UIView)

UIView 的實例即視圖,負責在屏幕上定義1個矩形區域,同時處理該區域的繪制和觸屏事件。視圖可以嵌套,也能夠像圖層1樣進行疊加。

// // ViewController.swift // UIViewControllerSample // // Created by jinnchang on 15/4/8. // Copyright (c) 2015年 Jinn Chang. All rights reserved. // import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // 添加1個黑色 view let view = UIView(frame: CGRectMake(40, 40, self.view.frame.size.width - 80, self.view.frame.size.height - 80)) view.backgroundColor = UIColor.blackColor() self.view.addSubview(view) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

4、窗口(UIWindow)

UIWindow 管理和調和利用程序的顯示,雖然 UIWindow 繼承自 UIView,但通常不直接操作 UIWindow 對象中與視圖相干的屬性變量。
iOS 程序啟動終了后,創建的第1個視圖控件就是 UIWindow(創建的第1個對象是 UIApplication),接著創建控制器的 view,最后將控制器的 view 添加到 window 上,因而控制器的 view 就顯示在屏幕上了。1般情況下,利用程序只有1個 UIWindow 對象,即便有多個,也只有1個 UIWindow 可以接遭到用戶的觸屏事件。

(1)自定義1個 myWindow

let myWindow = UIWindow(frame: UIScreen.mainScreen().bounds)
(2)設置 myWindow 為主窗口并顯示出來(view 不能平空顯示,必須依賴 window)
myWindow.makeKeyAndVisible()
UIWindow 和 UIView  之間的關系也能夠想象成這樣1個場景:首先會有1個空的畫框(UIWindow),我們在畫框上放置1塊畫布(UIView),然后可以在這個畫布上進行繪畫。畫布上可能會被畫上各種元素,例如 UILabel、UIButton 等,這些元素其實也是1個又1個 UIView,它們會有1個層級關系管理,有點類似于 Photoshop 中圖層的概念,層級高的元素會覆蓋住層級低的元素,從而致使層級低的元素被部份或完全遮擋。

5、屏幕(UIScreen)

通過這個類我們可以獲得1些關于屏幕的信息,通經常使用來獲得屏幕尺寸。

// 返回帶有狀態欄的 Rect let screenBounds = UIScreen.mainScreen().bounds // 返回不含狀態欄的 Rect let viewBounds = UIScreen.mainScreen().applicationFrame

6、視圖控制器(UIViewController)

視圖控制器管理 UIView 實例的生命周期及資源的加載與釋放、處理由于裝備旋轉致使的界面旋轉、和和用于構建復雜用戶界面的高級導航對象進行交互。

下圖展現了 UIView、UIWindow、UIScreen、UIViewController 之間的層級關系:


想要完全了解 UIViewController 用法可以參考 Github 上關于 UIViewController 的示例:UIViewControllerSample

7、導航控制器(UINavigationController)

在介紹 UINavigationController 之前先介紹另外一個概念:容器(Container)。1個 app 可能有很多的 UIViewController 組成,這時候需要1個容器來對這些 UIViewController 進行管理。大部份的容器也是1個 UIViewController,如 UINavigationController、UITabBarController,也有少數不是 UIViewController,比如 UIPopoverController 等。
UINavigationController 繼承自 UIViewController,通常我們新建的工程是直接將視圖控制器添加到 window 上的,如果添加了 navigation 以后就多了1層。

// // AppDelegate.swift // UINavigationControllerSample // // Created by jinnchang on 15/4/8. // Copyright (c) 2015年 Jinn Chang. All rights reserved. // import UIKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. // 初始化 window window = UIWindow(frame: UIScreen.mainScreen().bounds) window?.backgroundColor = UIColor.grayColor() // 初始化 navigationController let viewController = ViewController(nibName: nil, bundle: nil) let navigationController = UINavigationController(rootViewController: viewController) // 設置 window 的根控制器為 navigationController window?.rootViewController = navigationController // 設置 window 為主窗口并顯示出來 window?.makeKeyAndVisible() return true } func applicationWillResignActive(application: UIApplication) { // 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. } func applicationDidEnterBackground(application: UIApplication) { // 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. } func applicationWillEnterForeground(application: UIApplication) { // 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. } func applicationDidBecomeActive(application: UIApplication) { // 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. } func applicationWillTerminate(application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } }
以下是 UINavigationController 的結構圖:

通過 UINavigationController 進行視圖切換的兩種經常使用方式:

(1)presentViewController

// 向上彈出視圖 let firstViewController = FirstViewController(nibName: nil, bundle: nil) self.navigationController?.presentViewController(firstViewController, animated: true, completion: nil)
// 退出視圖 self.dismissViewControllerAnimated(true, completion: nil)
(2)pushViewController
// 向左推出視圖 let secondViewController = SecondViewController(nibName: nil, bundle: nil) self.navigationController?.pushViewController(secondViewController, animated: true);
// 退出視圖 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

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲欧美日韩国产精品 | 欧美色啪| 久久久久久国产精品免费 | 国产成人精品男人免费 | 久久久久久久久一次 | 97久久久久国产精品嫩草影院 | 久久综合久久久久 | 国产三级中文字幕 | 国产精品一区二区三区免费 | free性欧美精品videos | 日韩一级淫片 | 一区二区三区在线 | 网站 | 国产美女视频一区二区二三区 | 国产色视频一区二区三区 | 牛牛精品国内免费一区 | 香蕉在线精品一区二区 | 国产上床视频 | 欧美日韩国产高清一区二区三区 | 国产亚洲综合久久 | 91精品一区二区三区在线播放 | 性一交一乱一伦一色一情 | 亚洲性色成人 | 成人欧美一区二区三区视频不卡 | 五月激情六月丁香 | 日本一级淫片免费放 | 亚洲国产天堂久久综合 | 欧美性色黄大片www喷水 | 免费国产成人α片 | 全黄冷激性性视频 | 日韩福利在线视频 | 综合欧美亚洲 | 日韩一级片在线免费观看 | 亚洲乱码中文 | 免费网站看v片在线香蕉 | 亚洲综合欧美日韩 | 国产精品麻豆高清在线观看 | 毛片69| 成人网久久 | 免费一级淫片aaa片毛片a级 | 成人在线观看视频免费 | 国产视频一二三区 |