2017-02-15 58 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") } 
相关问题