[Xcode 實際操做]3、視圖控制器-(3)使用UINavigationController視圖控制器

目錄:[Swift]Xcode實際操做html

本文將演示導航視圖控制器的使用。swift

選擇項目導航區的資源文件夾。須要導入兩張圖片,做爲選項卡控制器的圖標。app

【+】->【Import】->選擇圖片->【Open】ide

接着依次建立兩個視圖控制器類文件post

一、第一個視圖控制器類文件ui

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

Name:FirstSubViewControllerspa

Subclass:UIViewController代理

Language:Swiftrest

->【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 }

二、第二個視圖控制器類文件

【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 }
相關文章
相關標籤/搜索