我已经创建了一个应用程序,用红色 VC(根)和一个用蓝色 VC 说明的入职序列。在用户到达主应用程序(红色)之前,我想拦截导航 Controller 上的启动并检查用户是否已经登机。这样做的最佳模式是什么?目前,无论我在 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
以查看您是否有一个特定键的 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/