入职顺序的最佳模式(逻辑)是什么?

时间:2015-04-17 12:08:05

标签: ios swift

我创建了一个应用程序,用红色VC(root)说明,以及用蓝色VC说明的入门序列。在用户访问主应用程序(红色)之前,我想拦截导航控制器上的启动并检查用户是否已经在船上。这样做的最佳模式是什么?目前,无论我在NC中使用什么逻辑,或者我把它放在哪里,只要它是根VC,就会评估红色VC。使用具有根VC的入门序列的最佳模式/设置/逻辑是什么? (也许根VC不是必需的,我已经使用过它,因为它似乎可以改善相对于模态segue的启动时间)

enter image description here

更新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
}

1 个答案:

答案 0 :(得分:1)

这样的逻辑在applicationDidFinishLaunchingWithOptions:方法的App Delegate *中就能很好地存在。

您要查询NSUserDefaults以查看您是否拥有BOOL等特定密钥的firstRun值。 如果您这样做,则将蓝色VC设置为根视图控制器,否则您将设置红色VC并将BOOL的'firstValue'键保留为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...
}