2017-02-05 26 views
0

我有一些代碼只在應用程序第一次啓動時在我的故事板中顯示第一個視圖控制器。之後,我想跳過該頁面,直接進入每次發佈的第二個視圖。我在導航控制器中嵌入了第一個視圖(連接到第二個視圖)。Swift顯示視圖僅在首次啓動時使用導航控制器

我的問題是,第一次啓動後,當應用程序直接進入第二個視圖時,它顯示的視圖沒有頂部的導航欄,我不知道爲什麼。

在我的appdelegate:

func firstLaunchCheck(){ 
     let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore") 
    if launchedBefore{ 
     let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
     let initialView : UIViewController = storyboard.instantiateViewController(withIdentifier: "mainScreen") as UIViewController 
     self.window = UIWindow(frame: UIScreen.main.bounds) 
     self.window?.rootViewController = initialView 
     self.window?.makeKeyAndVisible() 

    } 
    else{ 

     UserDefaults.standard.set(true, forKey: "launchedBefore") 
    } 

} 

UPDATE: 彼時我只是改變其瀏覽器中嵌入導航控制器(不包括第一個),因爲它沒有意義,我在那裏。所以,現在它加載導航控制器

+0

@jcka vc2應嵌入到導航控制器中,第二個應該轉到導航控制器,其頂部vc是vc2。 – aircraft

+0

,因爲在第二次跳過具有導航欄的第一個視圖控制器。如果您想第二次顯示導航欄,則還必須將其添加到第二個視圖控制器上 – Ram

回答

0

SecondViewControllerUINavigationController層次添加的第一個發射後,看到navigationBar在上面你可以firstVC推SecondViewController如果launchedBefore是的appDelegate

let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController 
let navigationController = window.rootViewController as! UINavigationController 
navigationController?.pushViewController(secondVC, animated: false) 
0

假的你需要在UINavigationController中嵌入第二個視圖控制器,即「mainScreen」,然後使其成爲應用程序窗口的rootViewController。

let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
let navigationController = UINavigationController.init(rootViewController: storyboard.instantiateViewController(withIdentifier: "mainScreen")) 
self.window = UIWindow(frame: UIScreen.main.bounds) 
self.window?.rootViewController = initialView 
self.window?.makeKeyAndVisible() 
相關問題