11.3.UINavigationController

总结:页面的跳转,以及模态。

//一、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()

//每一个被导航视图控制器所管理的视图控制器都有一个navigationItem(这里面包含了左按钮,右按钮,中间标题,中间视图)

//设置导航栏的标题

navigationItem.title = "Setting"

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

//设置右边按钮

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

//设置导航栏左按钮leftBarButtonItem:(UIBarButtonItem)

navigationItem.leftBarButtonItem = leftBarBtn

navigationItem.rightBarButtonItem = rightBarBtn

// 设置左右item数组

//        navigationItem.leftBarButtonItems = [leftBarBtn,rightBarBtn]

//        navigationItem.rightBarButtonItems = [leftBarBtn,rightBarBtn]

//设置中间视图

let segment = UISegmentedControl(items: ["已接来电","未接来 dian"])

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

segment.selectedSegmentIndex = 1

//设置中间视图

navigationItem.titleView = segment

//导航栏( UINavigationBar)

//在本类中(视图控制器)访问navigationController就是获取到本视图控制器所在的导航视图控制器

//设置导航栏是否隐藏

navigationController?.navigationBarHidden = false

// 设置导航栏样式

navigationController?.navigationBar.barStyle = .Default

//背景颜色

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

//导航栏本身的颜色

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

//导航栏元素颜色 (左按钮,右按钮.........)

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

//导航栏半透明效果

navigationController?.navigationBar.translucent = false

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

myView.backgroundColor = UIColor.blueColor()

view.addSubview(myView)

//        navigationController的contentView显示的谁的View?

}

//跳转第二个控制器页面

func rightBtnAction(){

//(1) 创建第二个控制器

let secondVC = SecondViewController()

//(2)使用当前控制器所在的导航视图控制器跳转到第二个控制器pushViewController(进入到下一个页面)

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()

//设置页面颜色为白色

view.backgroundColor = UIColor.whiteColor()

//设置标题

navigationItem.title = "SecondVC"

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

navigationItem.leftBarButtonItem = leftBarBtn

let rightBtn = UIBarButtonItem(title: "进入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:将当前显示在棧顶的控制器出棧(回到上一个页面)

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: "进入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: "进入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("模态显示", forState: .Normal)

btn.backgroundColor = UIColor.cyanColor()

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

view.addSubview(btn)

}

//点击按钮模态显示第六个视图控制器

func presentToSix(){

//创建第六个视图控制器

let sixthVC = SixthViewController()

//模态显示,跟导航视图控制器没关系

//参数completion:模态显示完成之后要执行的闭包

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

//模态显示动作完成要执行的代码

print("模态动作已完成")

}

}

func popToRootViewController(){

//(1)popToRootViewControllerAnimated:回到根视图控制器

//        navigationController?.popToRootViewControllerAnimated(true)

//        (2)

//先获取到棧里所有的视图控制器

let viewControllers = navigationController?.viewControllers

//获取根视图控制器(因为根视图控制器是最先入棧,所以在第0个下标)

let rootVC:UIViewController = viewControllers![0]

//导航视图控制器返回到指定的视图控制器

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("模态消失", forState: .Normal)

modelBtn.backgroundColor = UIColor.blueColor()

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

view.addSubview(modelBtn)

}

func dismissViewcontroller(){

//    (1)第一种方式:模态过程不可定制化  dismissViewcontroller()

//(2)第二种方式:模态消失过程可定制化(需不需要动画,模态结束后执行代码段)

dismissViewControllerAnimated(true) { () -> Void in

print("模态消失动作已结束")

}

}

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 {

//根据一个根视图控制器创建一个导航视图控制器

let vc = ViewController()

let navc = UINavigationController(rootViewController: vc)

//将应用的根视图控制器设置为导航视图控制器

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

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容