ios - 加载特定 View

标签 ios swift

我对 iOS 开发整体来说有点陌生。我正在尝试创建一个期刊应用程序,但正在努力解决你们中的许多人可能比我更理解的问题。我正在尝试根据参数加载特定 View 。就我而言,在首次加载应用程序时,我想加载“设置” View 作为默认页面。但是在第一次加载之后(也就是用户设置了他们的个人资料或其他什么之后),我想加载“主” View (显示必要信息的页面,例如创建新的日记条目等)

目前我的 viewDidLoad 中有这段代码:

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let userDefaults = UserDefaults.standard
        if userDefaults.value(forKey: "firstTimeLoad") as? Bool != nil {
// FIXME: Add code which loads the setup view

            userDefaults.setValue(true, forKey: "firstTimeLoad")
            userDefaults.synchronize()
        } else if userDefaults.value(forKey: "firstTimeLoad") as? Bool == nil {
// FIXME: Add code which hides the Setup view and loads the Master view.
        }
    }

我尝试了一些方法,例如使用 loadView 加载特定 View ,但似乎没有任何效果。

最佳答案

啊,你应该通过 AppDelegate.swift 执行此操作

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let userDefaults = UserDefaults.standard
    //If you have key, push your master view.
    if userDefaults.value(forKey: "firstTimeLoad") as? Bool != nil {
        return loadMasterViewController()
    } else {
        userDefaults.setValue(true, forKey: "firstTimeLoad")
        userDefaults.synchronize()
    }
    return true
}

func loadMasterViewController() -> Bool {
    self.window = UIWindow(frame: UIScreen.main.bounds)
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let masterViewController = storyboard.instantiateViewController(withIdentifier: "MasterViewController")
    self.window?.rootViewController = masterViewController
    self.window?.makeKeyAndVisible()
    return true
}

关于ios - 加载特定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41475141/

相关文章:

ios - UITextView 中的文本超出其框架 Swift 3

android - Google Maps Places 不适用于 Android

ios - 如何使用sqlite数据库插入数据?

ios - 如何在 Swift 4 中删除 CoreData 的对象

Swift 日期时区问题

android - 以相同的格式从 iOS 和 Android 获取设备语言

ios - UICollectionView自定义单元格布局

ios - 解析 : undeclared type 'PFFile' (POD 1. 17.2)

ios - 滚动时未调用 sizeForItemAt

ios - 快速计算行程距离核心位置