Swift iOS : NavigationController

前端之家收集整理的这篇文章主要介绍了Swift iOS : NavigationController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

控制器NavigationController常用于用来做层次化UI导航。类名为UINavigationController。

NavigationController可以通过属性包含多个ViewController、一个UINavigationBar、一个可选的UIToolbar。

以一个共三层的ViewController的案例,来展示NavigationController的使用:

  1. 共三层层次化UI

  2. 每一级页面内有一个按钮,可以继续导航到下一级页面

  3. 每一个页面的导航条的左侧按钮可以返还到上一级

代码如下:

  1. import UIKit
  2. @UIApplicationMain
  3. class AppDelegate: UIResponder,UIApplicationDelegate {
  4. var window: UIWindow?
  5. var nav : Nav?
  6. func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  7. self.window = UIWindow(frame: UIScreen.main.bounds)
  8. nav = Nav()
  9. self.window!.rootViewController = nav
  10. self.window?.makeKeyAndVisible()
  11. return true
  12. }
  13. }
  14. class Nav: UINavigationController {
  15. var count = 0
  16. var label : UILabel!
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. self.view.backgroundColor = .white
  20. self.pushViewController(Level1(),animated: true)
  21. }
  22. }
  23. class Level1: UIViewController {
  24. var count = 0
  25. var label : UILabel!
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. self.title = "Level 1"
  29. self.view.backgroundColor = .white
  30. let button = UIButton(type: .system)
  31. button.frame = CGRect(x: 120,y: 100,width: 100,height: 50)
  32. button.setTitle("Dive Into 2",for: .normal)
  33. button.addTarget(self,action: #selector(Level1.buttonAction(_:)),for: .touchUpInside)
  34. view.addSubview(button)
  35. }
  36. func buttonAction(_ sender:UIButton!){
  37. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  38. appDelegate.nav?.pushViewController(Level2(),animated: true)
  39. }
  40. }
  41. class Level2: UIViewController {
  42. var count = 0
  43. var label : UILabel!
  44. override func viewDidLoad() {
  45. super.viewDidLoad()
  46. self.title = "Level 2"
  47. self.view.backgroundColor = .white
  48. let button = UIButton(type: .system)
  49. button.frame = CGRect(x: 120,height: 50)
  50. button.setTitle("Dive Into 3",for: .normal)
  51. button.addTarget(self,action: #selector(Level2.buttonAction(_:)),for: .touchUpInside)
  52. view.addSubview(button)
  53. }
  54. func buttonAction(_ sender:UIButton!){
  55. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  56. appDelegate.nav?.pushViewController(Level3(),animated: true)
  57. }
  58. }
  59. class Level3: UIViewController {
  60. var count = 0
  61. var label : UILabel!
  62. override func viewDidLoad() {
  63. super.viewDidLoad()
  64. self.title = "Level 3"
  65. self.view.backgroundColor = .white
  66. let button = UIButton(type: .system)
  67. button.frame = CGRect(x: 120,height: 50)
  68. button.setTitle("End",action: #selector(Level3.buttonAction(_:)),for: .touchUpInside)
  69. view.addSubview(button)
  70. }
  71. func buttonAction(_ sender:UIButton!){
  72. }
  73. }

解释下:

  1. 把Nav作为UINavigationController的子类,实例化此类并设置到window.rootViewController上

  2. 每一层都是继承于UIViewController,类名分别为Level1、Level2、Level3

  3. Nav类视图加载完成后,把第一层Level1压入导航视图

  4. 无需任何用户代码,UINavigationController本身默认提供页面的顶部条状区域,它被称为导航条(UINavigationBar)

  5. 点击此导航条的左侧按钮可以返还到上一级页面;导航条中间显示当前ViewController的title属性

猜你在找的Swift相关文章