[Xcode 实际操作]三、视图控制器-(3)使用UINavigationController视图控制器

目录:[Swift]Xcode实际操作

本文将演示导航视图控制器的使用。

选择项目导航区的资源文件夹。需要导入两张图片,作为选项卡控制器的图标。

【+】->【Import】->选择图片->【Open】

接着依次创建两个视图控制器类文件

1、第一个视图控制器类文件

【New File】->【Cocoa Touch Class】->【Next】

Name:FirstSubViewController

Subclass:UIViewController

Language:Swift

->【Create】

 1 import UIKit
 2 
 3 class FirstSubViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         //设置当前视图控制器,在导航视图控制器中显示的标题
 8         self.title = "First Page"
 9         //设置当前视图控制器的背景颜色为棕色
10         self.view.backgroundColor = UIColor.brown
11         //设置右上角导航控制器的样式和功能,
12         //当点击此导航按钮时,页面跳转至第二个视图控制器。
13         self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Next", 
14                                                     style: .plain,
15                                                     target: self,
16                                                     action: #selector(FirstSubViewController.nextPage))
17     }
18     
19     //添加一个视图控制器的跳转方法
20     @objc func nextPage()
21     {
22         //初始化第二个视图控制器对象
23         let viewController = SecondSubViewController()
24         //将第二个视图控制器,压入导航视图控制器。
25         self.navigationController?.pushViewController(viewController, animated: true)
26     }
27 }

2、第二个视图控制器类文件

【New File】->【Cocoa Touch Class】->【Next】

Name:SecondSubViewController

Subclass:UIViewController

Language:Swift

->【Create】

 1 import UIKit
 2 
 3 class SecondSubViewController: UIViewController {
 4 
 5     override func viewDidLoad() {
 6         super.viewDidLoad()
 7         // Do any additional setup after loading the view.        
 8         //设置当前视图控制器,在导航视图控制器中显示的标题
 9         self.title = "Second Page"
10         //设置当前视图控制器的背景颜色为紫色
11         self.view.backgroundColor = UIColor.purple
12     }
13 
14     override func didReceiveMemoryWarning() {
15         super.didReceiveMemoryWarning()
16         // Dispose of any resources that can be recreated.
17     }
18 }

打开【AppDelegate.swift】应用程序的代理文件

 1 import UIKit
 2 
 3 @UIApplicationMain
 4 class AppDelegate: UIResponder, UIApplicationDelegate {
 5 
 6     var window: UIWindow?
 7 
 8     //在应用启动完成的代理方法中,创建程序的入口。
 9     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
10         // Override point for customization after application launch.
11         //实例化第一个视图控制器对象
12         let viewController = FirstSubViewController()
13         //初始化导航视图控制器对象
14         //并将第一个视图控制器对象,作为导航根视图控制器。
15         let navigationController = UINavigationController(rootViewController: viewController)
16         
17         //将导航控制器对象,作为当前窗口的根视图控制器。
18         self.window?.rootViewController = navigationController
19         
20         return true
21     }
22 
23     func applicationWillResignActive(_ application: UIApplication) {
24         // 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.
25         // 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.
26     }
27 
28     func applicationDidEnterBackground(_ application: UIApplication) {
29         // 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.
30         // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
31     }
32 
33     func applicationWillEnterForeground(_ application: UIApplication) {
34         // 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.
35     }
36 
37     func applicationDidBecomeActive(_ application: UIApplication) {
38         // 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.
39     }
40 
41     func applicationWillTerminate(_ application: UIApplication) {
42         // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
43     }
44 }
原文地址:https://www.cnblogs.com/strengthen/p/9968834.html