如何有条件地选择要导航到的下一个视图控制器? [英] How do I conditionally select the next view controller to navigate to?

查看:20
本文介绍了如何有条件地选择要导航到的下一个视图控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 iOS 应用程序中,我希望用户在第 1 页上使用 facebook 登录,然后重定向到第 2 页.在第 2 页上,有一个是"和否"按钮.现在有两种情况:

In my iOS app, I want the user to log in with facebook on page1, then get redirected to page2. On page2, there is a "Yes" and "No" button. Now, there are two scenarios:

1-如果用户按否",他会停留在第 2 页.如果他退出应用程序并在以后再次登录,他将再次转到第 2 页,直到他最终按下是".

1-If user pushes "No" he stays on page2. If he logs out from app and logs in back in future again, he again goes to page2 until he pushes "Yes" eventually.

2-用户在第 2 页上按是".现在,他被重定向到 page3.从现在开始,每次用户注销并重新登录时,他都会直接重定向到 page3,而不会看到 page2.这是我无法理解如何在我的代码中实现的部分.有没有办法在我的ViewController.swift"代码开始时初始化一个全局变量,但是如果用户按下是",在处理操作的代码部分用新值更新它的值?我的意思是可以在初始化时将变量的值从一件事更新到另一件事吗?

2-User pushes "Yes" on page2. Now, he gets redirected to page3. From now on, every time user logs out and logs back in, he gets redirected to page3 directly without seeing page2. This is the part I am having trouble understanding how to implement in my code. Is there a way to initialize a global variable at start of my "ViewController.swift" code, but update its' value with a new value at the part of the code that takes care of actions if user pushes "Yes"? I mean is it possible to update the value of a variable from one thing to another at initialization?

实际上,也许可以用更简短的方式来解释我的问题:我可以先用某个值快速初始化一个变量,然后,在用户执行特定操作(例如按下按钮)后,我会在此之后永久更改初始化值行动?

Actually, maybe the shorter way to explain my question is: Could I initialize a variable in swift with some value at first, then, after user performs a specific action such a pushing a button, I change the initialization value permanently afterwards that action?

另外,如果您知道任何其他方法可以实现此目的,请告诉我.

Also, please do let me know if you know any other ways to accomplish this.

推荐答案

当然可以.我会说这是用户的偏好.幸运的是,有一个叫做 NSUserDefaults 的东西.这是一个持久存储,因此值将永远保留在此处(直到应用被卸载或重建).

Yes of course you can. I would say it's a user preference. Lucky, there is something called NSUserDefaults for that. It's a persistent storage so the value will stay here forever (until the app is uninstalled or rebuilt).

例如,如果用户在第 2 页上说是,您会写:

For example if user say yes on page 2 you write:

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "usedSaidYes")

否则:

if NSUserDefaults.standardUserDefaults().boolForKey("usedSaidYes") == true {
     // Redirect page 3
} else {
     // Redirect page 2
}

您应该检查此值之前是否存在,如果不存在,请转到第 1 页

You should check if this value exist before, if not you go on page 1

这篇关于如何有条件地选择要导航到的下一个视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆