2017-02-15 63 views
-1

我有5個viewControllers(A,B,C,D,E),所有這些viewControllers都以編程方式連接,我可以成功地在它們之間推送和彈出。IOS - 如何在不同場景下的viewControllers之間跳轉

使用:

navigationController?.pushViewController(loginVc, animated: true) 
_ = navigationController?.popViewController(animated: true) 

注:的ViewController A出現第一作爲默認初始的viewController。

現在,我想要的是,當用戶安裝應用程序時,只有第一次viewController A必須顯示爲初始viewController,其餘時間的應用程序打開時,viewController D必須是初始viewController和從那裏我應該能夠在以前的viewControllers之間跳轉。我如何實現這一點。即時通訊使用Xcode 8.2,Swift 3.0

在此先感謝。

+0

你? '只有在第一次viewController A必須顯示爲初始viewController時,應用程序打開時的其餘時間纔有答案。顯示你的嘗試代碼 –

+0

你需要簡單地定義你的navigationController的rootViewController之前,應用程序可見,最好在AppDelegate didFinishLaunching方法。 – iphonic

+0

我很抱歉沒有提供代碼。背後有理由。我認爲這個解釋很清楚,可以給我一個解決方案,我從Florensvb那裏得到它。其他答案也很有幫助。感謝stackoverflow社區:) –

回答

3

爲了做到這一點,你根本就一個布爾添加到您的NSUserDefaults的,使用下面的代碼:

let defaults = UserDefaults.standard 
    if (!defaults.bool(forKey: "firstTime")) { //will be false if does not exist yet 
     navigationController?.pushViewController(yourDesiredVC, animated: true) //push desired vc 
     defaults.set(true, forKey: "firstTime") //set the key so it never executes again 
    } else { 
     navigationController?.pushViewController(yourDesiredVC, animated: true) //push the other vc 
    } 
+0

我希望這會工作。什麼是添加此代碼的正確位置? –

+0

@KarthikeyanSk嘿,只需把它放在你想推到新視圖控制器的地方! – Florensvb

+0

@NiravD,感謝編輯我的輸入錯誤 – Florensvb

2

你的問題並沒有真正說非常沒有一些代碼,但一個建議(給定當前質量的問題)將使用UserDefaults。將您當前版本的應用添加到名爲例如LatestVersion。如果它們不匹配,則在啓動時將其與應用程序當前版本進行比較,如果不顯示ViewControllerB,則顯示ViewControllerA

另一種方法是保存launchedForFirstTime。如果它沒有設置顯示ViewControllerA,但是上述內容將考慮您可能想要顯示該視圖的應用程序的未來版本。

2

你也可以在UserDefaults值來跟蹤回訪用戶,並檢查它是否存在:

if let returning :Bool = UserDefaults.standard.bool(forKey: "initial_controller_shown") { 
    //returning user flow 
    } else { 
    //new user flow 
    } 

一個共同的地方來檢查這是在applicationDidBecomeActive或didFinishLaunchingWithOptions

+0

如果你添加了'applicationDidBecomeActive',當用戶回到任何狀態時它將被調用, –

0

你是使用一個導航控制器,這將很難實現你的行爲。這將是您更容易使用獨立的視圖控制器的視圖A和d,並使用給他們打電話:

present(vc, animated: true) 

和罷免呼籲:

dismiss(animated: true) 
2

推出首當一次你的應用程序,然後使用標誌並存儲一些值,以便下次你的應用程序運行,然後你可以檢查用戶是否第一次訪問該應用程序..現在之後,去AppDelegate並將下面的代碼粘貼到DidFinishLaunchingWithOption中...

if yourFlag == true 
{  
let mainStoryboard: UIStoryboard = UIStoryboard(name: "MainStoreyBoard", bundle: nil) 
    let controller = mainStoryboard.instantiateViewController(withIdentifier: "StoreyBoardIdofYourViewController") as! UINavigationController 

    self.window?.rootViewController = controller 
} 

這將啓動d視圖 - 控制.....

0

您可以檢查它在你的應用程序delegate.m文件的應用是否安裝了第一次的ViewController A將顯示爲初始視圖控制器其他視圖控制器D。檢查它在以下功能:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
//check the initial view controller here// 
     return true 
    } 

斯威夫特3:

let launchBefore = UserDefaults.standard.bool(forKey: "launchBefore") 
if launchBefore { 
    print("Not first launch.") } else { 
    print("First launch, setting UserDefault.") 
    UserDefaults.standard.set(true, forKey: "launchBefore") } 
相關問題