11.3.UINavigationController

總結(jié):頁(yè)面的跳轉(zhuǎn),以及模態(tài)。

//一、ViewController.swift:

//

//? ViewController.swift

//? UINavigationController

//

//? Created by SZT on 2016/11/2.

//? Copyright ? 2016年 SZT. All rights reserved.

//

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

//每一個(gè)被導(dǎo)航視圖控制器所管理的視圖控制器都有一個(gè)navigationItem(這里面包含了左按鈕,右按鈕,中間標(biāo)題,中間視圖)

//設(shè)置導(dǎo)航欄的標(biāo)題

navigationItem.title = "Setting"

let leftBarBtn = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: "leftBtnAction")

//設(shè)置右邊按鈕

let rightBarBtn = UIBarButtonItem(title: "next", style: UIBarButtonItemStyle.Plain, target: self, action: "rightBtnAction")

//設(shè)置導(dǎo)航欄左按鈕leftBarButtonItem:(UIBarButtonItem)

navigationItem.leftBarButtonItem = leftBarBtn

navigationItem.rightBarButtonItem = rightBarBtn

// 設(shè)置左右item數(shù)組

//? ? ? ? navigationItem.leftBarButtonItems = [leftBarBtn,rightBarBtn]

//? ? ? ? navigationItem.rightBarButtonItems = [leftBarBtn,rightBarBtn]

//設(shè)置中間視圖

let segment = UISegmentedControl(items: ["已接來(lái)電","未接來(lái) dian"])

segment.frame = CGRectMake(0, 0, 100, 30)

segment.selectedSegmentIndex = 1

//設(shè)置中間視圖

navigationItem.titleView = segment

//導(dǎo)航欄( UINavigationBar)

//在本類中(視圖控制器)訪問(wèn)navigationController就是獲取到本視圖控制器所在的導(dǎo)航視圖控制器

//設(shè)置導(dǎo)航欄是否隱藏

navigationController?.navigationBarHidden = false

// 設(shè)置導(dǎo)航欄樣式

navigationController?.navigationBar.barStyle = .Default

//背景顏色

navigationController?.navigationBar.backgroundColor = UIColor.cyanColor()

//導(dǎo)航欄本身的顏色

navigationController?.navigationBar.barTintColor = UIColor.yellowColor()

//導(dǎo)航欄元素顏色 (左按鈕,右按鈕.........)

navigationController?.navigationBar.tintColor = UIColor.redColor()

//導(dǎo)航欄半透明效果

navigationController?.navigationBar.translucent = false

let myView = UIView(frame: CGRectMake(0,0,150,150))

myView.backgroundColor = UIColor.blueColor()

view.addSubview(myView)

//? ? ? ? navigationController的contentView顯示的誰(shuí)的View?

}

//跳轉(zhuǎn)第二個(gè)控制器頁(yè)面

func rightBtnAction(){

//(1) 創(chuàng)建第二個(gè)控制器

let secondVC = SecondViewController()

//(2)使用當(dāng)前控制器所在的導(dǎo)航視圖控制器跳轉(zhuǎn)到第二個(gè)控制器pushViewController(進(jìn)入到下一個(gè)頁(yè)面)

navigationController?.pushViewController(secondVC, animated: true)

}

func leftBtnAction(){

print("click left Btn")

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

//二、SecondViewController.swift:

//

//? SecondViewController.swift

//? UINavigationController

//

//? Created by SZT on 2016/11/3.

//? Copyright ? 2016年 SZT. All rights reserved.

//

import UIKit

class SecondViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

//設(shè)置頁(yè)面顏色為白色

view.backgroundColor = UIColor.whiteColor()

//設(shè)置標(biāo)題

navigationItem.title = "SecondVC"

let leftBarBtn = UIBarButtonItem(title: "back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")

navigationItem.leftBarButtonItem = leftBarBtn

let rightBtn = UIBarButtonItem(title: "進(jìn)入3", style: UIBarButtonItemStyle.Plain, target: self, action: "pushToThirdVC")

navigationItem.rightBarButtonItem = rightBtn

}

func pushToThirdVC(){

let thirdVC = ThirdViewController()

navigationController?.pushViewController(thirdVC, animated: true)

}

func backAction(btn:UIBarButtonItem){

print("返回")

//將SecondVc出棧popViewControllerAnimated:將當(dāng)前顯示在棧頂?shù)目刂破鞒鰲?回到上一個(gè)頁(yè)面)

navigationController?.popViewControllerAnimated(true)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

/*

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// Get the new view controller using segue.destinationViewController.

// Pass the selected object to the new view controller.

}

*/

}

//三、ThirdViewController.swift:

//

//? ThirdViewController.swift

//? UINavigationController

//

//? Created by SZT on 2016/11/3.

//? Copyright ? 2016年 SZT. All rights reserved.

//

import UIKit

class ThirdViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

navigationItem.title = "thirdVC"

let rightBtn = UIBarButtonItem(title: "進(jìn)入4", style: UIBarButtonItemStyle.Plain, target: self, action: "pushToFourthVC")

navigationItem.rightBarButtonItem = rightBtn

}

func pushToFourthVC(){

let fourthVC = FourthViewController()

navigationController?.pushViewController(fourthVC, animated: true)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

/*

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// Get the new view controller using segue.destinationViewController.

// Pass the selected object to the new view controller.

}

*/

}

//四、FourthViewController.swift:

//

//? FourthViewController.swift

//? UINavigationController

//

//? Created by SZT on 2016/11/3.

//? Copyright ? 2016年 SZT. All rights reserved.

//

import UIKit

class FourthViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

navigationItem.title = "fourthVC"

let rightBtn = UIBarButtonItem(title: "進(jìn)入5", style: UIBarButtonItemStyle.Plain, target: self, action: "pushToFitthVC")

navigationItem.rightBarButtonItem = rightBtn

}

func pushToFitthVC(){

let fifthVC = FifthViewController()

navigationController?.pushViewController(fifthVC, animated: true)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

/*

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// Get the new view controller using segue.destinationViewController.

// Pass the selected object to the new view controller.

}

*/

}

//五、FifthViewController.swift:

//

//? FifthViewController.swift

//? UINavigationController

//

//? Created by SZT on 2016/11/3.

//? Copyright ? 2016年 SZT. All rights reserved.

//

import UIKit

class FifthViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

view.backgroundColor = UIColor.whiteColor()

navigationItem.title = "FifthVC"

let leftBtn = UIBarButtonItem(title: "backToRoot", style: UIBarButtonItemStyle.Plain, target: self, action: "popToRootViewController")

navigationItem.leftBarButtonItem = leftBtn

let btn = UIButton(frame: CGRectMake(100,130,80,45))

btn.setTitle("模態(tài)顯示", forState: .Normal)

btn.backgroundColor = UIColor.cyanColor()

btn.addTarget(self, action: "presentToSix", forControlEvents: .TouchUpInside)

view.addSubview(btn)

}

//點(diǎn)擊按鈕模態(tài)顯示第六個(gè)視圖控制器

func presentToSix(){

//創(chuàng)建第六個(gè)視圖控制器

let sixthVC = SixthViewController()

//模態(tài)顯示,跟導(dǎo)航視圖控制器沒(méi)關(guān)系

//參數(shù)completion:模態(tài)顯示完成之后要執(zhí)行的閉包

presentViewController(sixthVC, animated: true) { () -> Void in

//模態(tài)顯示動(dòng)作完成要執(zhí)行的代碼

print("模態(tài)動(dòng)作已完成")

}

}

func popToRootViewController(){

//(1)popToRootViewControllerAnimated:回到根視圖控制器

//? ? ? ? navigationController?.popToRootViewControllerAnimated(true)

//? ? ? ? (2)

//先獲取到棧里所有的視圖控制器

let viewControllers = navigationController?.viewControllers

//獲取根視圖控制器(因?yàn)楦晥D控制器是最先入棧,所以在第0個(gè)下標(biāo))

let rootVC:UIViewController = viewControllers![0]

//導(dǎo)航視圖控制器返回到指定的視圖控制器

navigationController?.popToViewController(rootVC, animated: true)

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

/*

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// Get the new view controller using segue.destinationViewController.

// Pass the selected object to the new view controller.

}

*/

}

//六、SixthViewController.swift:

//

//? SixthViewController.swift

//? UINavigationController

//

//? Created by SZT on 2016/11/3.

//? Copyright ? 2016年 SZT. All rights reserved.

//

import UIKit

class SixthViewController: UIViewController {

override func viewDidLoad() {

super.viewDidLoad()

view.backgroundColor = UIColor.grayColor()

let modelBtn = UIButton(frame: CGRectMake(80,150,100,45))

modelBtn.setTitle("模態(tài)消失", forState: .Normal)

modelBtn.backgroundColor = UIColor.blueColor()

modelBtn.addTarget(self, action: "dismissViewcontroller", forControlEvents: .TouchUpInside)

view.addSubview(modelBtn)

}

func dismissViewcontroller(){

//? ? (1)第一種方式:模態(tài)過(guò)程不可定制化? dismissViewcontroller()

//(2)第二種方式:模態(tài)消失過(guò)程可定制化(需不需要?jiǎng)赢嫞B(tài)結(jié)束后執(zhí)行代碼段)

dismissViewControllerAnimated(true) { () -> Void in

print("模態(tài)消失動(dòng)作已結(jié)束")

}

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

/*

// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

// Get the new view controller using segue.destinationViewController.

// Pass the selected object to the new view controller.

}

*/

}

//七、AppDelegate.swift:

//

//? AppDelegate.swift

//? UINavigationController

//

//? Created by SZT on 2016/11/2.

//? Copyright ? 2016年 SZT. All rights reserved.

//

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

//根據(jù)一個(gè)根視圖控制器創(chuàng)建一個(gè)導(dǎo)航視圖控制器

let vc = ViewController()

let navc = UINavigationController(rootViewController: vc)

//將應(yīng)用的根視圖控制器設(shè)置為導(dǎo)航視圖控制器

window = UIWindow(frame: UIScreen.mainScreen().bounds)

window?.rootViewController = navc

window?.backgroundColor = UIColor.whiteColor()

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:.

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容