代码创建UINavigationController
首先,需要在项目设置去掉 Main Interface
在代码中自定义UIWindow对象,并设置控制器,注意,我这里用到的控制器都在关联了xib的
//
// AppDelegate.swift
// 代码创建导航
//
// Created by zhang on 16/1/16.
// Copyright © 2016年 jin. 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.
// 先取消项目设置中的 Main Interface
// 初始化窗口对象
let window = UIWindow()
// 初始化 navigation 对象
// 初始化默认显示的控制器
let mainController = UIViewControllerA()
// 初始化 navigation
// 这种方式设置初始化需要后面设置下默认控制器
// let navigation = UINavigationController()
// 在初始化的时候指定默认控制器
let navigation = UINavigationController(rootViewController: mainController)
// 下面的是两种设置 navigation 默认控制器的方法
// navigation.addChildViewController(mainController)
// navigation.pushViewController(mainController, animated: true)
// 设置当前窗口的根控制器
window.rootViewController = navigation
self.window = window
// 当前窗口作为主窗口并显示
self.window?.makeKeyAndVisible()
return true
}
}
navigation的根控制器
//
// UIViewControllerA.swift
// 代码创建导航
//
// Created by zhang on 16/1/16.
// Copyright © 2016年 jin. All rights reserved.
//
import UIKit
class UIViewControllerA: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 设置导航栏标题
self.title = "第一个控制器"
// 导航条左边的按钮
let addButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: nil, action: nil)
let cameraButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Camera, target: nil, action: nil)
// 赋值
self.navigationItem.leftBarButtonItems = [addButton,cameraButton]
// 设置导航栏右边的按钮
let composeButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Compose, target: nil, action: nil)
let actionButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: nil, action: nil)
self.navigationItem.rightBarButtonItems = [composeButton,actionButton]
// 设置返回
let backButton = UIBarButtonItem(title: "返回", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
self.navigationItem.backBarButtonItem = backButton
// Do any additional setup after loading the view.
}
@IBAction func nextController(sender: UIButton) {
// let controller = UIViewControllerB(nibName: "UIViewControllerB", bundle : nil)
let controller = UIViewControllerB()
// 把下个控制器对象加入 navigation 的栈,因为navigavtion会显示栈顶的控制器,所以,相当于跳转到下个控制器
self.navigationController?.pushViewController(controller, animated: true)
}
}
第二个控制器
//
// UIViewControllerB.swift
// 代码创建导航
//
// Created by zhang on 16/1/16.
// Copyright © 2016年 jin. All rights reserved.
//
import UIKit
class UIViewControllerB: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "第二个控制器"
// Do any additional setup after loading the view.
}
// 返回
@IBAction func back(sender: AnyObject) {
// 移除当前控制器,回到上一个,相当于返回
self.navigationController?.popViewControllerAnimated(true)
}
// 跳转到下个controller
@IBAction func next(sender: AnyObject) {
let controller = UIViewControllerC()
self.navigationController?.pushViewController(controller, animated: true)
}
}
第三个控制器
//
// UIViewControllerC.swift
// 代码创建导航
//
// Created by zhang on 16/1/16.
// Copyright © 2016年 jin. All rights reserved.
//
import UIKit
class UIViewControllerC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "第三个控制器"
// 这只 item titleview
self.navigationItem.titleView = UIImageView(image: UIImage(imageLiteral: "1"))
// Do any additional setup after loading the view.
}
@IBAction func back(sender: AnyObject) {
// 回到上一个控制器
self.navigationController?.popViewControllerAnimated(true)
}
@IBAction func backRoot(sender: AnyObject) {
// 回到根控制器
self.navigationController?.popToRootViewControllerAnimated(true)
}
}