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

标签 ios swift

我已经创建了一个应用程序,用红色 VC(根)和一个用蓝色 VC 说明的入职序列。在用户到达主应用程序(红色)之前,我想拦截导航 Controller 上的启动并检查用户是否已经登机。这样做的最佳模式是什么?目前,无论我在 NC 中有什么逻辑,或者我把它放在哪里,只要红色 VC 是根 VC,它总是被评估。使用根 VC 的入职序列的最佳模式/设置/逻辑是什么? (也许根 VC 不是必需的,我已经使用它,因为它似乎可以改善相对于模式转场的启动时间)

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
}

最佳答案

这样的逻辑在 App Delegate* 的 applicationDidFinishLaunchingWithOptions: 方法中会很好。

您将查询 NSUserDefaults 以查看您是否有一个特定键的 BOOL 值,例如 firstRun。 如果这样做,则将蓝色 VC 设置为 Root View Controller ,否则,您将设置红色 VC 并将“firstValue”键的 BOOL 保留到 NSUserDefaults

*人们会告诉您,用逻辑填充 App Delegate 是一件坏事,他们是对的,但是在设置 View 层次结构之前调用它是正确的地方。

编辑:这是一些代码。我正在写我的头顶,所以它可能需要调整才能工作......

- (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...
}

关于ios - 入职顺序的最佳模式(逻辑)是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29698777/

相关文章:

ios - xcode 显示来自 json 网络服务的数据

ios - 错误为 "The operation couldn’ 的 NSData 无法完成。 ( cocoa 错误 256。)”

ios - iPhone 上设置的电子邮件签名在其他客户端中呈现为更大的字体大小

iOS:在主从应用程序的导航栏中添加搜索栏

ios - statusCode 应该是 200,但是是 415

swift - AudioKit AKWaveTable 内存泄漏

ios - 之间传递数据

iphone - IOS - UITextField [键入时在 sqlite 数据库中搜索]

ios - 标识符为 backgroundSession 的后台 URLSession 已经存在

ios - Swift:为距离设置速度