加入序列的最佳模式(逻辑)是什么?
问题描述:
我已经创建了一个应用程序,用红色的VC(根)来说明,以及用蓝色的VC说明的入门序列。在用户到达主应用程序(红色)之前,我想拦截导航控制器中的启动并检查用户是否已加入。这样做的最佳模式是什么?目前,无论我在NC中拥有什么逻辑,或者在何处放置它,只要它是根VC,就会对红色VC进行评估。根VC使用入门序列的最佳模式/设置/逻辑是什么? (也许根本VC是没有必要的,我已经使用它,因为它似乎工作提高相对发射时间到模态赛格瑞)加入序列的最佳模式(逻辑)是什么?
更新1:这里是SWIFT代码 - 它的工作原理
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let isOnboarded:Bool = NSUserDefaults.standardUserDefaults().boolForKey("Onboarded")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// instantiate your desired ViewController
let dashboardViewController = storyboard.instantiateViewControllerWithIdentifier("DashboardVC") as! UIViewController
let onboardingViewControllerOne = storyboard.instantiateViewControllerWithIdentifier("OnboardingVCOne") as! UIViewController
let window = self.window
if (isOnboarded) {
window!.rootViewController = dashboardViewController
}else{
window!.rootViewController = onboardingViewControllerOne
}
return true
}
答
这样的逻辑可以很好地适用于App Delegate *中的applicationDidFinishLaunchingWithOptions:
方法。
你会查询NSUserDefaults
,看看你是否有值为firstRun
的特定键。 如果你这样做,那么你将蓝色的VC设置为根视图控制器,如果不是的话,你会设置红色的,并且将'firstValue'键的BOOL
保留为NSUserDefaults
。
*人们会告诉你用逻辑填充App Delegate是一件坏事,而且他们是对的,但是这是正确的,因为它是在设置视图层次结构之前调用的。
编辑:这是一些代码。我正在注销我的头顶,所以它可能需要调整工作...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *blue = [UIViewController new];
UIViewController *red = [UIViewController new];
BOOL isFirstLaunch = [[NSUserDefaults standardDefaults] boolForKey:@"firstRun"];
UIWindow *window = self.window;
if (firstRun)
{
self.window.rootViewController = blue;
}
else
{
self.window.rootViewController = red;
[[NSUserDefaults standardDefaults] setBool:YES forKey:@"firstRun"];
}
//...rest of method...
}
ahh好的,我明白了,那很聪明。你写道:“...如果不是,你会设置红色的,并且将'firstValue'键的BOOL持久化为NSUserDefaults。”你坚持什么意思?你能否详细说明一些代码?我应该能够找到如何设置VC作为根VC – KML
当然让我编辑答案。我也会说,阅读苹果公司在这类事情上的一些东西对你来说是件好事,所以你对这些概念感到更加自在。 – Cocoadelica
好的,做完了,让我知道你是否还有其他问题。如果您可以接受答案,如果您对此感到满意,那将会很棒:) – Cocoadelica