ios - 如何避免在 swift 上打开 View Controller ?

标签 ios swift

我正在使用 Swift 开发适用于 iOS 的应用程序。我有两个 View Controller ;第一个用于配置应用程序(它必须仅在应用程序第一次启动时出现),第二个 View Controller 用于登录应用程序。

问题是:我想在第一次启动时显示“配置 Controller ”,然后必须出现“登录 Controller ”,但我不知道该怎么做。我试图将条件放入“配置 Controller ”以强制第二个(登录)使其工作。但它不起作用。

你能给我解释一下关于这个话题的一些事情吗?

最佳答案

您可以使用这段代码来设置您希望哪个 View Controller 成为在 AppDelegate.h 启动时显示的第一个 View Controller 。请记住在 Main.storyboard 中为您的 View Controller 编辑 Storyboard ID 并相应地调整字符串

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let storyboard = UIStoryboard(name: "MainStoryboard", bundle: NSBundle.mainBundle())
    let defaults = NSUserDefaults.standardUserDefaults()

    var rootViewController : UIViewController;

    if (defaults.boolForKey("HasBeenLaunched")) {
        // This gets executed if the app has ALREADY been launched
        rootViewController = storyboard.instantiateViewControllerWithIdentifier(/*storyboard id of your login controller*/) as UIViewController
    } else {
        // This gets executed if the app has NEVER been launched
        defaults.setBool(true, forKey: "HasBeenLaunched")
        defaults.synchronize()

        rootViewController = storyboard.instantiateViewControllerWithIdentifier(/*storyboard id of your configuration controller*/) as UIViewController
    }

    self.window?.rootViewController = rootViewController;
    self.window?.makeKeyAndVisible();

    return true
}

一旦您的应用程序完成启动并即将显示其初始 View Controller ,就会调用此函数。要更好地了解 if 正在检查什么,请查看 NSUserDefaults 的文档(这是一个非常有用的类)。 return do 之前的最后两个语句基本上是显示在 if 中选择的 View Controller 。

希望这对您有所帮助。

关于ios - 如何避免在 swift 上打开 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26791726/

相关文章:

ios - objective-c 中带有长数的pow操作

ios - QuickBlox 视频聊天 : QBRequest. logInWithUserEmail 与 QBChat.instance().connectWithUser

swift - Xcode 编译器错误未在编辑器中突出显示

保存前创建对象的 iOS 模式?

iOS-表面检测

ios - MVC - 没有 UIKit 的模型文件中的 NSFetchRequest

ios - 当与 iPad 配合使用时,branch.io 的通用链接无法与 iPhone 配合使用

ios - 无法将项目添加到 UIView xib 中的 UICollectionView

ios - Firebase 身份验证 token ,检查是否有效

ios - 以编程方式模拟 Swift 中的滑动手势